]> git.pld-linux.org Git - packages/crossnacl-newlib.git/blob - pthread.h
- pl, use arch*dir macros
[packages/crossnacl-newlib.git] / pthread.h
1 /*
2  * Copyright (c) 2012 The Native Client Authors. All rights reserved.
3  * Use of this source code is governed by a BSD-style license that can be
4  * found in the LICENSE file.
5  */
6
7 /** @file
8  * Defines the API in the
9  * <a href="group___pthread.html">Pthread library</a>
10  *
11  * @addtogroup Pthread
12  * @{
13  */
14
15 #ifndef _PTHREAD_H
16 #define _PTHREAD_H 1
17
18 #include <stdint.h>
19 #include <sched.h>
20 #include <time.h>
21 #include <sys/nacl_nice.h>
22 #include <sys/queue.h>
23 #include <sys/types.h>
24
25 /*
26  * Signed 32-bit integer supporting CompareAndSwap and AtomicIncrement
27  * (see implementations), as well as atomic loads and stores.
28  * Instances must be naturally aligned.
29  */
30 typedef int AtomicInt32;
31
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35
36 struct timespec;
37 struct __nc_basic_thread_data;
38
39 /** Mutex type attributes */
40 enum {
41   /** Fast mutex type; for use with pthread_mutexattr_settype() */
42   PTHREAD_MUTEX_FAST_NP,
43   /** Recursive mutex type; for use with pthread_mutexattr_settype() */
44   PTHREAD_MUTEX_RECURSIVE_NP,
45   /** Error-checking mutex type; for use with pthread_mutexattr_settype() */
46   PTHREAD_MUTEX_ERRORCHECK_NP,
47   PTHREAD_MUTEX_NORMAL = PTHREAD_MUTEX_FAST_NP,
48   PTHREAD_MUTEX_RECURSIVE = PTHREAD_MUTEX_RECURSIVE_NP,
49   PTHREAD_MUTEX_ERRORCHECK = PTHREAD_MUTEX_ERRORCHECK_NP,
50
51   PTHREAD_MUTEX_DEFAULT = PTHREAD_MUTEX_NORMAL
52 };
53
54 /*
55  * The layout of pthread_mutex_t and the static initializers are redefined
56  * in newlib's sys/lock.h file (including this file from sys/lock.h will
57  * cause include conflicts). When changing one of the definitions, make sure to
58  * change the second one.
59  */
60 /**
61  * A structure representing a thread mutex. It should be considered an
62  * opaque record; the names of the fields can change anytime.
63  */
64 typedef struct {
65   /*
66    * mutex_state is either UNLOCKED (0), LOCKED_WITHOUT_WAITERS (1) or
67    * LOCKED_WITH_WAITERS (2).  See "enum MutexState".
68    */
69   volatile int mutex_state;
70
71   /**
72    * The kind of mutex:
73    * PTHREAD_MUTEX_FAST_NP, PTHREAD_MUTEX_RECURSIVE_NP,
74    * or PTHREAD_MUTEX_ERRORCHECK_NP
75    */
76   int mutex_type;
77
78   /** ID of the thread that owns the mutex */
79   struct __nc_basic_thread_data *owner_thread_id;
80
81   /** Recursion depth counter for recursive mutexes */
82   uint32_t recursion_counter;
83
84   /*
85    * Padding is for compatibility with libraries (newlib etc.) that
86    * were built before libpthread switched to using futexes, and to
87    * match _LOCK_T in newlib's newlib/libc/include/sys/lock.h.
88    */
89   int unused_padding;
90 } pthread_mutex_t;
91
92 /**
93  * A structure representing mutex attributes. It should be considered an
94  * opaque record and accessed only using pthread_mutexattr_settype().
95  */
96 typedef struct {
97   /**
98    * The kind of mutex:
99    * PTHREAD_MUTEX_FAST_NP, PTHREAD_MUTEX_RECURSIVE_NP,
100    * or PTHREAD_MUTEX_ERRORCHECK_NP
101    */
102   int kind;
103 } pthread_mutexattr_t;
104
105 /**
106  * A structure representing a condition variable. It should be considered an
107  * opaque record; the names of the fields can change anytime.
108  */
109 typedef struct {
110   /* This is incremented on each pthread_cond_signal/broadcast() call. */
111   int sequence_number;
112
113   /*
114    * Padding is for compatibility with libraries (newlib etc.) that
115    * were built before libpthread switched to using futexes.
116    */
117   int unused_padding;
118 } pthread_cond_t;
119
120 /**
121  * A structure representing condition variable attributes. Currently
122  * Native Client condition variables have no attributes.
123  */
124 typedef struct {
125   int dummy; /**< Reserved; condition variables don't have attributes */
126 } pthread_condattr_t;
127
128 /** A value that represents an uninitialized handle. */
129 #define NC_INVALID_HANDLE -1
130
131 /** Maximum valid thread ID value. */
132 #define MAX_THREAD_ID (0xfffffffe)
133
134 /** Illegal thread ID value. */
135 #define NACL_PTHREAD_ILLEGAL_THREAD_ID ((pthread_t) 0)
136
137 /** Statically initializes a pthread_mutex_t representing a recursive mutex. */
138 #define PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP \
139     {0, 1, NACL_PTHREAD_ILLEGAL_THREAD_ID, 0, NC_INVALID_HANDLE}
140 /** Statically initializes a pthread_mutex_t representing a fast mutex. */
141 #define PTHREAD_MUTEX_INITIALIZER \
142     {0, 0, NACL_PTHREAD_ILLEGAL_THREAD_ID, 0, NC_INVALID_HANDLE}
143 /**
144  * Statically initializes a pthread_mutex_t representing an
145  * error-checking mutex.
146  */
147 #define PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP \
148     {0, 2, NACL_PTHREAD_ILLEGAL_THREAD_ID, 0, NC_INVALID_HANDLE}
149 /** Statically initializes a condition variable (pthread_cond_t). */
150 #define PTHREAD_COND_INITIALIZER {0, NC_INVALID_HANDLE}
151
152
153
154 /* Functions for mutex handling.  */
155
156 /** @nqPosix
157  * Initializes a mutex using attributes in mutex_attr, or using the
158  * default values if the latter is NULL.
159  *
160  * @linkPthread
161  *
162  * @param mutex The address of the mutex structure to be initialized.
163  * @param mutex_attr The address of the attributes structure.
164  *
165  * @return 0 upon success, 1 otherwise
166  */
167 extern int pthread_mutex_init(pthread_mutex_t *mutex,
168                               const pthread_mutexattr_t *mutex_attr);
169
170 /** @nqPosix
171 * Destroys a mutex.
172 *
173 * @linkPthread
174 *
175 * @param mutex The address of the mutex structure to be destroyed.
176 *
177 * @return 0 upon success, non-zero error code otherwise
178 */
179 extern int pthread_mutex_destroy(pthread_mutex_t *mutex);
180
181 /** @nqPosix
182 * Tries to lock a mutex.
183 *
184 * @linkPthread
185 *
186 * @param mutex The address of the mutex structure to be locked.
187 *
188 * @return 0 upon success, EBUSY if the mutex is locked by another thread,
189 * non-zero error code otherwise.
190 */
191 extern int pthread_mutex_trylock(pthread_mutex_t *mutex);
192
193 /** @nqPosix
194 * Locks a mutex.
195 *
196 * @linkPthread
197 *
198 * @param mutex The address of the mutex structure to be locked.
199 *
200 * @return 0 upon success, non-zero error code otherwise.
201 */
202 extern int pthread_mutex_lock(pthread_mutex_t *mutex);
203
204 /* Wait until lock becomes available, or specified time passes. */
205 extern int pthread_mutex_timedlock(pthread_mutex_t *mutex,
206                                    struct timespec *abstime);
207
208 /** @nqPosix
209 * Unlocks a mutex.
210 *
211 * @linkPthread
212 *
213 * @param mutex The address of the mutex structure to be unlocked.
214 *
215 * @return 0 upon success, non-zero error code otherwise.
216 */
217 extern int pthread_mutex_unlock(pthread_mutex_t *mutex);
218
219 /* Mutex attributes manipulation */
220
221 /** @nqPosix
222 * Initializes mutex attributes.
223 *
224 * @linkPthread
225 *
226 * @param attr The address of the attributes structure to be initialized.
227 *
228 * @return 0.
229 */
230 extern int pthread_mutexattr_init(pthread_mutexattr_t *attr);
231
232 /** @nqPosix
233 * Destroys mutex attributes structure.
234 *
235 * @linkPthread
236 *
237 * @param attr The address of the attributes structure to be destroyed.
238 *
239 * @return 0.
240 */
241 extern int pthread_mutexattr_destroy(pthread_mutexattr_t *attr);
242
243 /** @nqPosix
244 * Sets the mutex type: fast, recursive or error-checking.
245 *
246 * @linkPthread
247 *
248 * @param attr The address of the attributes structure.
249 * @param kind PTHREAD_MUTEX_FAST_NP, PTHREAD_MUTEX_RECURSIVE_NP or
250 * PTHREAD_MUTEX_ERRORCHECK_NP.
251 *
252 * @return 0 on success, -1 for illegal values of kind.
253 */
254 extern int pthread_mutexattr_settype(pthread_mutexattr_t *attr,
255                                      int kind);
256
257 /** @nqPosix
258 * Gets the mutex type: fast, recursive or error-checking.
259 *
260 * @linkPthread
261 *
262 * @param attr The address of the attributes structure.
263 * @param kind Pointer to the location where the mutex kind value is copied.
264 *
265 * @return 0.
266 */
267 extern int pthread_mutexattr_gettype(const pthread_mutexattr_t *attr,
268                                      int *kind);
269
270 /* Functions for handling conditional variables.  */
271
272 /** @nqPosix
273 * Initializes a condition variable.
274 *
275 * @linkPthread
276 *
277 * @param cond Pointer to the condition variable structure.
278 * @param cond_attr Pointer to the attributes structure, should be NULL as
279 * Native Client does not support any attributes for a condition variable at
280 * this stage.
281 *
282 * @return 0 for success, 1 otherwise.
283 */
284 extern int pthread_cond_init(pthread_cond_t *cond,
285                              const pthread_condattr_t *cond_attr);
286
287 /** @nqPosix
288 * Destroys a condition variable.
289 *
290 * @linkPthread
291 *
292 * @param cond Pointer to the condition variable structure.
293 *
294 * @return 0 for success, non-zero error code otherwise.
295 */
296 extern int pthread_cond_destroy(pthread_cond_t *cond);
297
298 /** @nqPosix
299 * Signals a condition variable, waking up one of the threads waiting on it.
300 *
301 * @linkPthread
302 *
303 * @param cond Pointer to the condition variable structure.
304 *
305 * @return 0 for success, non-zero error code otherwise.
306 */
307 extern int pthread_cond_signal(pthread_cond_t *cond);
308
309 /** @nqPosix
310 * Wakes up all threads waiting on a condition variable.
311 *
312 * @linkPthread
313 *
314 * @param cond Pointer to the condition variable structure.
315 *
316 * @return 0 for success, non-zero error code otherwise.
317 */
318 extern int pthread_cond_broadcast(pthread_cond_t *cond);
319
320 /** @nqPosix
321 * Waits for a condition variable to be signaled or broadcast.
322 *
323 * @linkPthread
324 *
325 * @param cond Pointer to the condition variable structure.
326 * @param mutex Pointer to the mutex structure. The mutex is assumed to be locked
327 * when this function is called.
328 *
329 * @return 0 for success, non-zero error code otherwise.
330 */
331 extern int pthread_cond_wait(pthread_cond_t *cond,
332                              pthread_mutex_t *mutex);
333
334 /** @nqPosix
335 * Waits for condition variable cond to be signaled or broadcast until
336 * abstime.
337 *
338 * @linkPthread
339 *
340 * @param cond Pointer to the condition variable structure.
341 * @param mutex Pointer to the mutex structure. The mutex is assumed to be locked
342 * when this function is called.
343 * @param abstime Absolute time specification; zero is the beginning of the epoch
344 * (00:00:00 GMT, January 1, 1970).
345 *
346 * @return 0 for success, non-zero error code otherwise.
347 */
348 int pthread_cond_timedwait_abs(pthread_cond_t *cond,
349                                pthread_mutex_t *mutex,
350                                const struct timespec *abstime);
351
352  /** @nqPosix
353 * Waits for condition variable cond to be signaled or broadcast; wait time is
354 * limited by reltime.
355 *
356 * @linkPthread
357 *
358 * @param cond Pointer to the condition variable structure.
359 * @param mutex Pointer to the mutex structure. The mutex is assumed to be locked
360 * when this function is called.
361 * @param reltime Time specification, relative to the current time.
362 *
363 * @return 0 for success, non-zero error code otherwise.
364 */
365 int pthread_cond_timedwait_rel(pthread_cond_t *cond,
366                                pthread_mutex_t *mutex,
367                                const struct timespec *reltime);
368
369 /**
370  * Defined for POSIX compatibility; pthread_cond_timedwait() is actually
371  * a macro calling pthread_cond_timedwait_abs().
372  */
373 #define pthread_cond_timedwait pthread_cond_timedwait_abs
374
375
376 /* Threads */
377 /** Thread entry function type. */
378 typedef void *(*nc_thread_function)(void *p);
379 /** Thread identifier type. */
380 typedef struct __nc_basic_thread_data *pthread_t;
381
382 /** A structure representing thread attributes. */
383 typedef struct {
384   int joinable; /**< 1 if the thread is joinable, 0 otherwise */
385   size_t stacksize; /**< The requested thread stack size in bytes. */
386 } pthread_attr_t;
387
388
389 /** Joinable thread type; for use with pthread_attr_setdetachstate(). */
390 #define PTHREAD_CREATE_JOINABLE 1
391 /** Detached thread type; for use with pthread_attr_setdetachstate(). */
392 #define PTHREAD_CREATE_DETACHED 0
393
394 /** Minimum stack size; for use with pthread_attr_setstacksize(). */
395 #define PTHREAD_STACK_MIN     (1024)
396
397 /* default stack size */
398 #define PTHREAD_STACK_DEFAULT (512 * 1024)
399
400 /* Thread functions  */
401
402 /** @nqPosix
403 * Creates a thread.
404 *
405 * @linkPthread
406 *
407 * @param[out] thread_id A pointer to the location where the identifier of the
408 * newly created thread is stored on success.
409 * @param attr Thread attributes structure.
410 * @param start_routine Thread function.
411 * @param arg A single argument that is passed to the thread function.
412 *
413 * @return 0 for success, non-zero error code otherwise.
414 */
415 extern int pthread_create(pthread_t *thread_id,
416                           const pthread_attr_t *attr,
417                           void *(*start_routine)(void *p),
418                           void *arg);
419
420 /** @nqPosix
421 * Obtains the identifier of the current thread.
422 *
423 * @linkPthread
424 *
425 * @return Thread ID of the current thread.
426 */
427 extern pthread_t pthread_self(void);
428
429 /** @nqPosix
430 * Compares two thread identifiers.
431 *
432 * @linkPthread
433 *
434 * @param thread1 Thread ID of thread A.
435 * @param thread2 Thread ID of thread B.
436 *
437 * @return 1 if both identifiers belong to the same thread, 0 otherwise.
438 */
439 extern int pthread_equal(pthread_t thread1, pthread_t thread2);
440
441 /** @nqPosix
442 * Terminates the calling thread.
443 *
444 * @linkPthread
445 *
446 * @param retval Return value of the thread.
447 *
448 * @return The function never returns.
449 */
450 extern void pthread_exit(void *retval);
451
452 /** @nqPosix
453 * Makes the calling thread wait for termination of another thread.
454 *
455 * @linkPthread
456 *
457 * @param th The identifier of the thread to wait for.
458 * @param thread_return If not NULL, points to the location where the return
459 * value of the terminated thread is stored upon completion.
460 *
461 * @return 0 on success, non-zero error code otherwise.
462 */
463 extern int pthread_join(pthread_t th, void **thread_return);
464
465 /** @nqPosix
466 * Indicates that the specified thread is never to be joined with pthread_join().
467 * The resources of that thread will therefore be freed immediately when it
468 * terminates, instead of waiting for another thread to perform pthread_join()
469 * on it.
470 *
471 * @linkPthread
472 *
473 * @param th Thread identifier.
474 *
475 * @return 0 on success, non-zero error code otherwise.
476 */
477 extern int pthread_detach(pthread_t th);
478
479 /** @nqPosix
480 * Sends a signal to a thread.  (Currently only a stub implementation.)
481 *
482 * @linkPthread
483 *
484 * @param thread_id The identifier of the thread to receive the signal.
485 * @param sig The signal value to send.
486 *
487 * @return 0 for success, non-zero error code otherwise.
488 */
489 extern int pthread_kill(pthread_t thread_id,
490                         int sig);
491
492 /* Functions for handling thread attributes.  */
493 /** @nqPosix
494 * Initializes thread attributes structure attr with default attributes
495 * (detachstate is PTHREAD_CREATE_JOINABLE).
496 *
497 * @linkPthread
498 *
499 * @param attr Pointer to thread attributes structure.
500 *
501 * @return 0 on success, non-zero error code otherwise.
502 */
503 extern int pthread_attr_init(pthread_attr_t *attr);
504
505 /** @nqPosix
506 * Destroys a thread attributes structure.
507 *
508 * @linkPthread
509 *
510 * @param attr Pointer to thread attributes structure.
511 *
512 * @return 0 on success, non-zero error code otherwise.
513 */
514 extern int pthread_attr_destroy(pthread_attr_t *attr);
515
516 /** @nqPosix
517 * Sets the detachstate attribute in thread attributes.
518 *
519 * @linkPthread
520 *
521 * @param attr Pointer to thread attributes structure.
522 * @param detachstate Value to be set, determines whether the thread is joinable.
523 *
524 * @return 0 on success, non-zero error code otherwise.
525 */
526 extern int pthread_attr_setdetachstate(pthread_attr_t *attr,
527                                        int detachstate);
528
529 /** @nqPosix
530 * Gets the detachstate attribute from thread attributes.
531 *
532 * @linkPthread
533 *
534 * @param attr Pointer to thread attributes structure.
535 * @param detachstate Location where the value of `detachstate` is stored upon
536 * successful completion.
537 *
538 * @return 0 on success, non-zero error code otherwise.
539 */
540 extern int pthread_attr_getdetachstate(pthread_attr_t *attr,
541                                        int *detachstate);
542
543 /** @nqPosix
544 * Sets the stacksize attribute in thread attributes.  Has no effect if the
545 * size is less than PTHREAD_STACK_MIN.
546 *
547 * @linkPthread
548 *
549 * @param attr Pointer to thread attributes structure.
550 * @param stacksize Value to be set, determines the minimum stack size.
551 *
552 * @return 0 on success, non-zero error code otherwise.
553 */
554 extern int pthread_attr_setstacksize(pthread_attr_t *attr,
555                                      size_t stacksize);
556
557 /** @nqPosix
558 * Gets the stacksize attribute in thread attributes.
559 *
560 * @linkPthread
561 *
562 * @param attr Pointer to thread attributes structure.
563 * @param stacksize Value to be set, determines the minimum stack size.
564 *
565 * @return 0 on success, non-zero error code otherwise.
566 */
567 extern int pthread_attr_getstacksize(pthread_attr_t *attr,
568                                      size_t *stacksize);
569
570 /** @nqPosix
571  * Gets the maximum address of the stack (assuming stacks grow
572  * downwards) of the given thread.
573  *
574  * If the given thread exits concurrently with the call to this
575  * function, the behaviour is undefined.
576  *
577  * Note that in the future this may be removed and replaced with an
578  * implementation of pthread_getattr_np(), for consistency with Linux
579  * glibc.  pthread_getattr_np() + pthread_attr_getstack() return the
580  * stack base (minimum) address and stack size.  However, that is
581  * currently unimplementable under NaCl, because NaCl does not provide
582  * a way to determine the initial thread's stack size.  See:
583  * https://code.google.com/p/nativeclient/issues/detail?id=3431
584  */
585 extern int pthread_get_stack_end_np(pthread_t tid, void **stack_end);
586
587 /* Functions for handling thread-specific data.  */
588
589 /** Thread-specific key identifier type */
590 typedef int pthread_key_t;
591
592 /** Number of available keys for thread-specific data. */
593 #define PTHREAD_KEYS_MAX 512
594
595 /** @nqPosix
596 * Creates a key value identifying a location in the thread-specific
597 * data area.
598 *
599 * @linkPthread
600 *
601 * @param key Pointer to the location where the value of the key is stored upon
602 * successful completion.
603 * @param destr_function Pointer to a cleanup function that is called if the
604 * thread terminates while the key is still allocated.
605 *
606 * @return 0 on success, non-zero error code otherwise.
607 */
608 extern int pthread_key_create(pthread_key_t *key,
609                               void (*destr_function)(void *p));
610
611
612 /** @nqPosix
613 * Destroys a thread-specific data key.
614 *
615 * @linkPthread
616 *
617 * @param key Key value, previously obtained using pthread_key_create().
618 *
619 * @return 0 on success, non-zero error code otherwise.
620 */
621 extern int pthread_key_delete(pthread_key_t key);
622
623 /** @nqPosix
624 * Stores a value in the thread-specific data slot identified by a key value.
625 *
626 * @linkPthread
627 *
628 * @param key Key value, previously obtained using pthread_key_create().
629 * @param pointer The value to be stored.
630 *
631 * @return 0 on success, non-zero error code otherwise.
632 */
633 extern int pthread_setspecific(pthread_key_t key,
634                                const void *pointer);
635
636 /** @nqPosix
637 * Gets the value currently stored at the thread-specific data slot
638 * identified by the key.
639 *
640 * @linkPthread
641 *
642 * @param key Key value, previously obtained using pthread_key_create().
643 *
644 * @return The value that was previously stored using pthread_setspecific is
645 * returned on success, otherwise NULL.
646 */
647 extern void *pthread_getspecific(pthread_key_t key);
648
649 /**
650  * A structure describing a control block
651  * used with the pthread_once() function.
652  * It should be considered an opaque record;
653  * the names of the fields can change anytime.
654  */
655 typedef struct {
656   /** A flag: 1 if the function was already called, 0 if it wasn't */
657   AtomicInt32      done;
658
659   /** Synchronization lock for the flag */
660   pthread_mutex_t lock;
661 } pthread_once_t;
662
663 /** Static initializer for pthread_once_t. */
664 #define PTHREAD_ONCE_INIT {0, PTHREAD_MUTEX_INITIALIZER}
665
666 /** @nqPosix
667 * Ensures that a piece of initialization code is executed at most once.
668 *
669 * @linkPthread
670 *
671 * @param __once_control Points to a static or extern variable statically
672 * initialized to PTHREAD_ONCE_INIT.
673 * @param __init_routine A pointer to the initialization function.
674 *
675 * @return 0.
676 */
677 extern int pthread_once(pthread_once_t *__once_control,
678                         void (*__init_routine)(void));
679
680 /** @nqPosix
681 * Sets the scheduling priority of a thread.
682 *
683 * @linkPthread
684 *
685 * @param thread_id Identifies the thread to operate on.
686 * @param prio Scheduling priority to apply to that thread.
687 *
688 * @return 0 on success, non-zero error code otherwise.
689 */
690 extern int pthread_setschedprio(pthread_t thread_id, int prio);
691
692 /*
693  * NOTE: this is only declared here to shut up
694  * some warning in the c++ system header files.
695  * We do not define this function anywhere.
696  */
697
698 extern int pthread_cancel(pthread_t th);
699
700 /*
701  * NOTE: There are only stub implementations of these functions.
702  */
703
704 void pthread_cleanup_push(void (*func)(void *cleanup_arg), void *arg);
705 void pthread_cleanup_pop(int execute);
706
707 /**
708 * @} End of PTHREAD group
709 */
710
711 #ifdef __cplusplus
712 }
713 #endif
714
715 #endif  /* pthread.h */
This page took 0.110398 seconds and 3 git commands to generate.