/usr/lib/ocaml/lwt/lwt_unix.h is in liblwt-ocaml-dev 2.7.1-4build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 | /* Lightweight thread library for OCaml
* http://www.ocsigen.org/lwt
* Header lwt_unix
* Copyright (C) 2010 Jérémie Dimino
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, with linking exceptions;
* either version 2.1 of the License, or (at your option) any later
* version. See COPYING file for details.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
* 02111-1307, USA.
*/
#ifndef __LWT_UNIX_H
#define __LWT_UNIX_H
#define CAML_NAME_SPACE
#include <lwt_config.h>
#include <caml/mlvalues.h>
#include <caml/unixsupport.h>
#include <caml/socketaddr.h>
/* The macro to get the file-descriptor from a value. */
#if defined(LWT_ON_WINDOWS)
# define FD_val(value) win_CRT_fd_of_filedescr(value)
#else
# define FD_val(value) Int_val(value)
#endif
/* Macro to extract a libev loop from a caml value. */
#define Ev_loop_val(value) *(struct ev_loop**)Data_custom_val(value)
/* +-----------------------------------------------------------------+
| Utils |
+-----------------------------------------------------------------+ */
/* Allocate the given amount of memory and abort the program if there
is no free memory left. */
void *lwt_unix_malloc(size_t size);
void *lwt_unix_realloc(void *ptr, size_t size);
/* Same as [strdup] and abort hte program if there is not memory
left. */
char *lwt_unix_strdup(char *string);
/* Helpers for allocating structures. */
#define lwt_unix_new(type) (type*)lwt_unix_malloc(sizeof(type))
#define lwt_unix_new_plus(type, size) (type*)lwt_unix_malloc(sizeof(type) + size)
/* Raise [Lwt_unix.Not_available]. */
void lwt_unix_not_available(char const *feature) Noreturn;
#define LWT_NOT_AVAILABLE1(prim) \
CAMLprim value lwt_ ## prim(value a1) \
{ lwt_unix_not_available(#prim); }
#define LWT_NOT_AVAILABLE2(prim) \
CAMLprim value lwt_ ## prim(value a1, value a2) \
{ lwt_unix_not_available(#prim); }
#define LWT_NOT_AVAILABLE3(prim) \
CAMLprim value lwt_ ## prim(value a1, value a2, value a3) \
{ lwt_unix_not_available(#prim); }
#define LWT_NOT_AVAILABLE4(prim) \
CAMLprim value lwt_ ## prim(value a1, value a2, value a3, value a4) \
{ lwt_unix_not_available(#prim); }
#define LWT_NOT_AVAILABLE5(prim) \
CAMLprim value lwt_ ## prim(value a1, value a2, value a3, value a4, value a5) \
{ lwt_unix_not_available(#prim); }
#define LWT_NOT_AVAILABLE6(prim) \
CAMLprim value lwt_ ## prim(value a1, value a2, value a3, value a4, value a5, value a6) \
{ lwt_unix_not_available(#prim); }
/* +-----------------------------------------------------------------+
| Notifications |
+-----------------------------------------------------------------+ */
/* Sends a notification for the given id. */
void lwt_unix_send_notification(intnat id);
/* +-----------------------------------------------------------------+
| Threading |
+-----------------------------------------------------------------+ */
#if defined(HAVE_PTHREAD)
#include <pthread.h>
typedef pthread_t lwt_unix_thread;
typedef pthread_mutex_t lwt_unix_mutex;
typedef pthread_cond_t lwt_unix_condition;
#else
typedef DWORD lwt_unix_thread;
typedef CRITICAL_SECTION lwt_unix_mutex;
typedef struct lwt_unix_condition lwt_unix_condition;
#endif
/* Launch a thread in detached mode. */
void lwt_unix_launch_thread(void* (*start)(void*), void* data);
/* Return a handle to the currently running thread. */
lwt_unix_thread lwt_unix_thread_self();
/* Returns whether two thread handles refer to the same thread. */
int lwt_unix_thread_equal(lwt_unix_thread thread1, lwt_unix_thread thread2);
/* Initialises a mutex. */
void lwt_unix_mutex_init(lwt_unix_mutex *mutex);
/* Destroy a mutex. */
void lwt_unix_mutex_destroy(lwt_unix_mutex *mutex);
/* Lock a mutex. */
void lwt_unix_mutex_lock(lwt_unix_mutex *mutex);
/* Unlock a mutex. */
void lwt_unix_mutex_unlock(lwt_unix_mutex *mutex);
/* Initialises a condition variable. */
void lwt_unix_condition_init(lwt_unix_condition *condition);
/* Destroy a condition variable. */
void lwt_unix_condition_destroy(lwt_unix_condition *condition);
/* Signal a condition variable. */
void lwt_unix_condition_signal(lwt_unix_condition *condition);
/* Broadcast a signal on a condition variable. */
void lwt_unix_condition_broadcast(lwt_unix_condition *condition);
/* Wait for a signal on a condition variable. */
void lwt_unix_condition_wait(lwt_unix_condition *condition, lwt_unix_mutex *mutex);
/* +-----------------------------------------------------------------+
| Detached jobs |
+-----------------------------------------------------------------+ */
/* How job are executed. */
enum lwt_unix_async_method {
/* Synchronously. */
LWT_UNIX_ASYNC_METHOD_NONE = 0,
/* Asynchronously, on another thread. */
LWT_UNIX_ASYNC_METHOD_DETACH = 1,
/* Asynchronously, on the main thread, switcing to another thread if
necessary. */
LWT_UNIX_ASYNC_METHOD_SWITCH = 2
};
/* Type of job execution modes. */
typedef enum lwt_unix_async_method lwt_unix_async_method;
/* State of a job. */
enum lwt_unix_job_state {
/* The job has not yet started. */
LWT_UNIX_JOB_STATE_PENDING,
/* The job is running. */
LWT_UNIX_JOB_STATE_RUNNING,
/* The job is done. */
LWT_UNIX_JOB_STATE_DONE
};
/* A job descriptor. */
struct lwt_unix_job {
/* The next job in the queue. */
struct lwt_unix_job *next;
/* Id used to notify the main thread in case the job do not
terminate immediately. */
intnat notification_id;
/* The function to call to do the work.
This function must not:
- access or allocate OCaml block values (tuples, strings, ...),
- call OCaml code. */
void (*worker)(struct lwt_unix_job *job);
/* The function to call to extract the result and free memory
allocated by the job.
Note: if you want to raise an excpetion, be sure to free
resources before raising it!
It has been introduced in Lwt 2.3.3. */
value (*result)(struct lwt_unix_job *job);
/* State of the job. */
enum lwt_unix_job_state state;
/* Is the main thread still waiting for the job ? */
int fast;
/* Mutex to protect access to [state] and [fast]. */
lwt_unix_mutex mutex;
/* Thread running the job. */
lwt_unix_thread thread;
/* The async method in used by the job. */
lwt_unix_async_method async_method;
};
/* Type of job descriptors. */
typedef struct lwt_unix_job* lwt_unix_job;
/* Type of worker functions. */
typedef void (*lwt_unix_job_worker)(lwt_unix_job job);
/* Type of result functions. */
typedef value (*lwt_unix_job_result)(lwt_unix_job job);
/* Allocate a caml custom value for the given job. */
value lwt_unix_alloc_job(lwt_unix_job job);
/* Free resourecs allocated for this job and free it. */
void lwt_unix_free_job(lwt_unix_job job);
/* +-----------------------------------------------------------------+
| Helpers for writing jobs |
+-----------------------------------------------------------------+ */
/* Allocate a job structure and set its worker and result fields.
- VAR is the name of the job variable. It is usually "job".
- FUNC is the suffix of the structure name and functions of this job.
It is usually the name of the function that is wrapped.
- SIZE is the dynamic size to allocate at the end of the structure,
in case it ends ends with something of the form: char data[]);
*/
#define LWT_UNIX_INIT_JOB(VAR, FUNC, SIZE) \
struct job_##FUNC *VAR = lwt_unix_new_plus(struct job_##FUNC, SIZE); \
VAR->job.worker = (lwt_unix_job_worker)worker_##FUNC; \
VAR->job.result = (lwt_unix_job_result)result_##FUNC
/* Same as LWT_UNIX_INIT_JOB, but also stores a string argument named
ARG at the end of the job structure. The offset of the copied
string is assigned to the field VAR->ARG.
The structure must ends with: char data[]; */
#define LWT_UNIX_INIT_JOB_STRING(VAR, FUNC, SIZE, ARG) \
mlsize_t __len = caml_string_length(ARG); \
LWT_UNIX_INIT_JOB(VAR, FUNC, SIZE + __len + 1); \
VAR->ARG = VAR->data + SIZE; \
memcpy(VAR->ARG, String_val(ARG), __len + 1)
/* Same as LWT_UNIX_INIT_JOB, but also stores two string arguments
named ARG1 and ARG2 at the end of the job structure. The offsets of
the copied strings are assigned to the fields VAR->ARG1 and
VAR->ARG2.
The structure definition must ends with: char data[]; */
#define LWT_UNIX_INIT_JOB_STRING2(VAR, FUNC, SIZE, ARG1, ARG2) \
mlsize_t __len1 = caml_string_length(ARG1); \
mlsize_t __len2 = caml_string_length(ARG2); \
LWT_UNIX_INIT_JOB(VAR, FUNC, SIZE + __len1 + __len2 + 2); \
VAR->ARG1 = VAR->data + SIZE; \
VAR->ARG2 = VAR->data + SIZE + __len1 + 1; \
memcpy(VAR->ARG1, String_val(ARG1), __len1 + 1); \
memcpy(VAR->ARG2, String_val(ARG2), __len2 + 1)
/* If TEST is true, it frees the job and raises Unix.Unix_error using
the value of errno stored in the field error_code. */
#define LWT_UNIX_CHECK_JOB(VAR, TEST, NAME) \
if (TEST) { \
int error_code = VAR->error_code; \
lwt_unix_free_job(&VAR->job); \
unix_error(error_code, NAME, Nothing); \
}
/* If TEST is true, it frees the job and raises Unix.Unix_error using
the value of errno stored in the field error_code and uses the C
string ARG for the third field of Unix.Unix_error. */
#define LWT_UNIX_CHECK_JOB_ARG(VAR, TEST, NAME, ARG) \
if (TEST) { \
int error_code = VAR->error_code; \
value arg = caml_copy_string(ARG); \
lwt_unix_free_job(&VAR->job); \
unix_error(error_code, NAME, arg); \
}
/* +-----------------------------------------------------------------+
| Deprecated |
+-----------------------------------------------------------------+ */
/* Define not implement methods. Deprecated: it is for the old
mechanism with three externals. */
#define LWT_UNIX_JOB_NOT_IMPLEMENTED(name) \
CAMLprim value lwt_unix_##name##_job(value Unit) \
{ \
caml_invalid_argument("not implemented"); \
} \
\
CAMLprim value lwt_unix_##name##_result(value Unit) \
{ \
caml_invalid_argument("not implemented"); \
} \
\
CAMLprim value lwt_unix_##name##_free(value Unit) \
{ \
caml_invalid_argument("not implemented"); \
}
#endif /* __LWT_UNIX_H */
|