zhangmeng
2024-04-22 16935f4aebffdd1b6580b844391a0aa0f4f3012b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
//
// Copyright 2021 Staysail Systems, Inc. <info@staysail.tech>
// Copyright 2018 Capitar IT Group BV <info@capitar.com>
//
// This software is supplied under the terms of the MIT License, a
// copy of which should be located in the distribution where this
// file was obtained (LICENSE.txt).  A copy of the license may also be
// found online at https://opensource.org/licenses/MIT.
//
 
#include <stdlib.h>
#include <string.h>
 
#include "core/nng_impl.h"
#include "nng/supplemental/util/platform.h"
 
nng_time
nng_clock(void)
{
    (void) nni_init();
    return (nni_clock());
}
 
// Sleep for specified msecs.
void
nng_msleep(nng_duration dur)
{
    (void) nni_init();
    nni_msleep(dur);
}
 
// Create and start a thread.  Note that on some platforms, this might
// actually be a coroutine, with limitations about what system APIs
// you can call.  Therefore, these threads should only be used with the
// I/O APIs provided by nng.  The thread runs until completion.
int
nng_thread_create(nng_thread **thrp, void (*func)(void *), void *arg)
{
    nni_thr *thr;
    int      rv;
 
    (void) nni_init();
 
    if ((thr = NNI_ALLOC_STRUCT(thr)) == NULL) {
        return (NNG_ENOMEM);
    }
    *thrp = (void *) thr;
    if ((rv = nni_thr_init(thr, func, arg)) != 0) {
        return (rv);
    }
    nni_thr_run(thr);
    return (0);
}
 
void
nng_thread_set_name(nng_thread *thr, const char *name)
{
    nni_thr_set_name((void *)thr, name);
}
 
// Destroy a thread (waiting for it to complete.)  When this function
// returns all resources for the thread are cleaned up.
void
nng_thread_destroy(nng_thread *thr)
{
    nni_thr *t = (void *) thr;
    nni_thr_fini(t);
    NNI_FREE_STRUCT(t);
}
 
struct nng_mtx {
    nni_mtx m;
};
 
int
nng_mtx_alloc(nng_mtx **mpp)
{
    nng_mtx *mp;
 
    (void) nni_init();
 
    if ((mp = NNI_ALLOC_STRUCT(mp)) == NULL) {
        return (NNG_ENOMEM);
    }
    nni_mtx_init(&mp->m);
    *mpp = mp;
    return (0);
}
 
void
nng_mtx_free(nng_mtx *mp)
{
    if (mp != NULL) {
        nni_mtx_fini(&mp->m);
        NNI_FREE_STRUCT(mp);
    }
}
 
void
nng_mtx_lock(nng_mtx *mp)
{
    nni_mtx_lock(&mp->m);
}
 
void
nng_mtx_unlock(nng_mtx *mp)
{
    nni_mtx_unlock(&mp->m);
}
 
struct nng_cv {
    nni_cv c;
};
 
int
nng_cv_alloc(nng_cv **cvp, nng_mtx *mx)
{
    nng_cv *cv;
 
    if ((cv = NNI_ALLOC_STRUCT(cv)) == NULL) {
        return (NNG_ENOMEM);
    }
    nni_cv_init(&cv->c, &mx->m);
    *cvp = cv;
    return (0);
}
 
void
nng_cv_free(nng_cv *cv)
{
    if (cv != NULL) {
        nni_cv_fini(&cv->c);
        NNI_FREE_STRUCT(cv);
    }
}
 
void
nng_cv_wait(nng_cv *cv)
{
    nni_cv_wait(&cv->c);
}
 
int
nng_cv_until(nng_cv *cv, nng_time when)
{
    return (nni_cv_until(&cv->c, (nni_time) when));
}
 
void
nng_cv_wake(nng_cv *cv)
{
    nni_cv_wake(&cv->c);
}
 
void
nng_cv_wake1(nng_cv *cv)
{
    nni_cv_wake1(&cv->c);
}
 
uint32_t
nng_random(void)
{
    (void) nni_init();
    return (nni_random());
}