|
| 1 | +/* |
| 2 | + * The contents of this file are subject to the Mozilla Public |
| 3 | + * License Version 1.1 (the "License"); you may not use this file |
| 4 | + * except in compliance with the License. You may obtain a copy of |
| 5 | + * the License at http://www.mozilla.org/MPL/ |
| 6 | + * |
| 7 | + * Software distributed under the License is distributed on an "AS |
| 8 | + * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or |
| 9 | + * implied. See the License for the specific language governing |
| 10 | + * rights and limitations under the License. |
| 11 | + * |
| 12 | + * The Original Code is the Netscape Portable Runtime library. |
| 13 | + * |
| 14 | + * The Initial Developer of the Original Code is Netscape |
| 15 | + * Communications Corporation. Portions created by Netscape are |
| 16 | + * Copyright (C) 1994-2000 Netscape Communications Corporation. All |
| 17 | + * Rights Reserved. |
| 18 | + * |
| 19 | + * Contributor(s): Silicon Graphics, Inc. |
| 20 | + * |
| 21 | + * Portions created by SGI are Copyright (C) 2000-2001 Silicon |
| 22 | + * Graphics, Inc. All Rights Reserved. |
| 23 | + * |
| 24 | + * Alternatively, the contents of this file may be used under the |
| 25 | + * terms of the GNU General Public License Version 2 or later (the |
| 26 | + * "GPL"), in which case the provisions of the GPL are applicable |
| 27 | + * instead of those above. If you wish to allow use of your |
| 28 | + * version of this file only under the terms of the GPL and not to |
| 29 | + * allow others to use your version of this file under the MPL, |
| 30 | + * indicate your decision by deleting the provisions above and |
| 31 | + * replace them with the notice and other provisions required by |
| 32 | + * the GPL. If you do not delete the provisions above, a recipient |
| 33 | + * may use your version of this file under either the MPL or the |
| 34 | + * GPL. |
| 35 | + */ |
| 36 | + |
| 37 | +#ifndef __ST_THREAD_H__ |
| 38 | +#define __ST_THREAD_H__ |
| 39 | + |
| 40 | +#include <unistd.h> |
| 41 | +#include <sys/types.h> |
| 42 | +#include <sys/socket.h> |
| 43 | +#include <sys/uio.h> |
| 44 | +#include <time.h> |
| 45 | +#include <errno.h> |
| 46 | +#include <poll.h> |
| 47 | + |
| 48 | +#define ST_VERSION "1.9" |
| 49 | +#define ST_VERSION_MAJOR 1 |
| 50 | +#define ST_VERSION_MINOR 9 |
| 51 | + |
| 52 | +/* Undefine this to remove the context switch callback feature. */ |
| 53 | +#define ST_SWITCH_CB |
| 54 | + |
| 55 | +#ifndef ETIME |
| 56 | + #define ETIME ETIMEDOUT |
| 57 | +#endif |
| 58 | + |
| 59 | +#ifndef ST_UTIME_NO_TIMEOUT |
| 60 | + #define ST_UTIME_NO_TIMEOUT ((st_utime_t) -1LL) |
| 61 | +#endif |
| 62 | + |
| 63 | +#ifndef ST_UTIME_NO_WAIT |
| 64 | + #define ST_UTIME_NO_WAIT 0 |
| 65 | +#endif |
| 66 | + |
| 67 | +#define ST_EVENTSYS_DEFAULT 0 |
| 68 | +#define ST_EVENTSYS_ALT 3 |
| 69 | + |
| 70 | +#ifdef __cplusplus |
| 71 | +extern "C" { |
| 72 | +#endif |
| 73 | + |
| 74 | +typedef unsigned long long st_utime_t; |
| 75 | +typedef struct _st_thread * st_thread_t; |
| 76 | +typedef struct _st_cond * st_cond_t; |
| 77 | +typedef struct _st_mutex * st_mutex_t; |
| 78 | +typedef struct _st_netfd * st_netfd_t; |
| 79 | +#ifdef ST_SWITCH_CB |
| 80 | +typedef void (*st_switch_cb_t)(void); |
| 81 | +#endif |
| 82 | + |
| 83 | +extern int st_init(void); |
| 84 | +extern int st_getfdlimit(void); |
| 85 | + |
| 86 | +extern int st_set_eventsys(int eventsys); |
| 87 | +extern int st_get_eventsys(void); |
| 88 | +extern const char *st_get_eventsys_name(void); |
| 89 | + |
| 90 | +#ifdef ST_SWITCH_CB |
| 91 | +extern st_switch_cb_t st_set_switch_in_cb(st_switch_cb_t cb); |
| 92 | +extern st_switch_cb_t st_set_switch_out_cb(st_switch_cb_t cb); |
| 93 | +#endif |
| 94 | + |
| 95 | +extern st_thread_t st_thread_self(void); |
| 96 | +extern void st_thread_exit(void *retval); |
| 97 | +extern int st_thread_join(st_thread_t thread, void **retvalp); |
| 98 | +extern void st_thread_interrupt(st_thread_t thread); |
| 99 | +extern void st_thread_yield(); |
| 100 | +extern st_thread_t st_thread_create(void *(*start)(void *arg), void *arg, int joinable, int stack_size); |
| 101 | +extern int st_randomize_stacks(int on); |
| 102 | +extern int st_set_utime_function(st_utime_t (*func)(void)); |
| 103 | + |
| 104 | +extern st_utime_t st_utime(void); |
| 105 | +extern st_utime_t st_utime_last_clock(void); |
| 106 | +extern int st_timecache_set(int on); |
| 107 | +extern time_t st_time(void); |
| 108 | +extern int st_usleep(st_utime_t usecs); |
| 109 | +extern int st_sleep(int secs); |
| 110 | +extern st_cond_t st_cond_new(void); |
| 111 | +extern int st_cond_destroy(st_cond_t cvar); |
| 112 | +extern int st_cond_timedwait(st_cond_t cvar, st_utime_t timeout); |
| 113 | +extern int st_cond_wait(st_cond_t cvar); |
| 114 | +extern int st_cond_signal(st_cond_t cvar); |
| 115 | +extern int st_cond_broadcast(st_cond_t cvar); |
| 116 | +extern st_mutex_t st_mutex_new(void); |
| 117 | +extern int st_mutex_destroy(st_mutex_t lock); |
| 118 | +extern int st_mutex_lock(st_mutex_t lock); |
| 119 | +extern int st_mutex_unlock(st_mutex_t lock); |
| 120 | +extern int st_mutex_trylock(st_mutex_t lock); |
| 121 | + |
| 122 | +extern int st_key_create(int *keyp, void (*destructor)(void *)); |
| 123 | +extern int st_key_getlimit(void); |
| 124 | +extern int st_thread_setspecific(int key, void *value); |
| 125 | +extern void *st_thread_getspecific(int key); |
| 126 | + |
| 127 | +extern st_netfd_t st_netfd_open(int osfd); |
| 128 | +extern st_netfd_t st_netfd_open_socket(int osfd); |
| 129 | +extern void st_netfd_free(st_netfd_t fd); |
| 130 | +extern int st_netfd_close(st_netfd_t fd); |
| 131 | +extern int st_netfd_fileno(st_netfd_t fd); |
| 132 | +extern void st_netfd_setspecific(st_netfd_t fd, void *value, void (*destructor)(void *)); |
| 133 | +extern void *st_netfd_getspecific(st_netfd_t fd); |
| 134 | +extern int st_netfd_serialize_accept(st_netfd_t fd); |
| 135 | +extern int st_netfd_poll(st_netfd_t fd, int how, st_utime_t timeout); |
| 136 | + |
| 137 | +extern int st_poll(struct pollfd *pds, int npds, st_utime_t timeout); |
| 138 | +extern st_netfd_t st_accept(st_netfd_t fd, struct sockaddr *addr, int *addrlen, st_utime_t timeout); |
| 139 | +extern int st_connect(st_netfd_t fd, const struct sockaddr *addr, int addrlen, st_utime_t timeout); |
| 140 | +extern ssize_t st_read(st_netfd_t fd, void *buf, size_t nbyte, st_utime_t timeout); |
| 141 | +extern ssize_t st_read_fully(st_netfd_t fd, void *buf, size_t nbyte, st_utime_t timeout); |
| 142 | +extern int st_read_resid(st_netfd_t fd, void *buf, size_t *resid, st_utime_t timeout); |
| 143 | +extern ssize_t st_readv(st_netfd_t fd, const struct iovec *iov, int iov_size, st_utime_t timeout); |
| 144 | +extern int st_readv_resid(st_netfd_t fd, struct iovec **iov, int *iov_size, st_utime_t timeout); |
| 145 | +extern ssize_t st_write(st_netfd_t fd, const void *buf, size_t nbyte, st_utime_t timeout); |
| 146 | +extern int st_write_resid(st_netfd_t fd, const void *buf, size_t *resid, st_utime_t timeout); |
| 147 | +extern ssize_t st_writev(st_netfd_t fd, const struct iovec *iov, int iov_size, st_utime_t timeout); |
| 148 | +extern int st_writev_resid(st_netfd_t fd, struct iovec **iov, int *iov_size, st_utime_t timeout); |
| 149 | +extern int st_recvfrom(st_netfd_t fd, void *buf, int len, struct sockaddr *from, int *fromlen, st_utime_t timeout); |
| 150 | +extern int st_sendto(st_netfd_t fd, const void *msg, int len, const struct sockaddr *to, int tolen, st_utime_t timeout); |
| 151 | +extern int st_recvmsg(st_netfd_t fd, struct msghdr *msg, int flags, st_utime_t timeout); |
| 152 | +extern int st_sendmsg(st_netfd_t fd, const struct msghdr *msg, int flags, st_utime_t timeout); |
| 153 | + |
| 154 | +// @see http://man7.org/linux/man-pages/man2/sendmmsg.2.html |
| 155 | +#include <sys/socket.h> |
| 156 | +struct st_mmsghdr { |
| 157 | + struct msghdr msg_hdr; /* Message header */ |
| 158 | + unsigned int msg_len; /* Number of bytes transmitted */ |
| 159 | +}; |
| 160 | +extern int st_sendmmsg(st_netfd_t fd, struct st_mmsghdr *msgvec, unsigned int vlen, int flags, st_utime_t timeout); |
| 161 | + |
| 162 | +extern st_netfd_t st_open(const char *path, int oflags, mode_t mode); |
| 163 | + |
| 164 | +#ifdef DEBUG |
| 165 | +extern void _st_show_thread_stack(st_thread_t thread, const char *messg); |
| 166 | +extern void _st_iterate_threads(void); |
| 167 | +#endif |
| 168 | + |
| 169 | +#ifdef __cplusplus |
| 170 | +} |
| 171 | +#endif |
| 172 | + |
| 173 | +#endif /* !__ST_THREAD_H__ */ |
| 174 | + |
0 commit comments