root/threads.h

/* [previous][next][first][last][top][bottom][index][help] [+1 threads.h] */

INCLUDED FROM


   1 /*
   2  * The MIT License (MIT)
   3  *
   4  * Copyright (c) 2014 Jan Adelsbach <jan@janadelsbach.com>
   5  *
   6  * Permission is hereby granted, free of charge, to any person obtaining a copy
   7  * of this software and associated documentation files (the "Software"), to deal
   8  * in the Software without restriction, including without limitation the rights
   9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10  * copies of the Software, and to permit persons to whom the Software is
  11  * furnished to do so, subject to the following conditions:
  12  * 
  13  * The above copyright notice and this permission notice shall be included in 
  14  * all copies or substantial portions of the Software.
  15  * 
  16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22  * SOFTWARE.
  23  */
  24 #ifndef THREADS_H
  25 #define THREADS_H
  26 
  27 #include <pthread.h>
  28 #include <time.h>
  29 
  30 #define ONCE_FLAG_INIT      PTHREAD_ONCE_INIT
  31 #define TSS_DTOR_ITERATIONS PTHREAD_DESTRUCTOR_ITERATIONS
  32 #define _Thread_local __thread
  33 
  34 typedef void (*tss_dtor_t)(void*);
  35 typedef int (*thrd_start_t)(void*);
  36 
  37 enum mtx_type {
  38   mtx_plain     = 0,
  39   mtx_try       = 1,
  40   mtx_timed     = 2,
  41   mtx_recursive = 4
  42 };
  43 
  44 enum thrd_status {
  45   thrd_success = 0,
  46   thrd_timeout = 1,
  47   thrd_error   = 2,
  48   thrd_busy    = 3,
  49   thrd_nomem   = 4
  50 }; 
  51 
  52 typedef pthread_cond_t  cnd_t;
  53 typedef pthread_t       thrd_t;
  54 typedef pthread_key_t   tss_t;
  55 typedef pthread_mutex_t mtx_t;
  56 typedef pthread_once_t  once_flag;
  57 
  58 int  mtx_init(mtx_t *mtx, int type);
  59 void mtx_destroy(mtx_t *mtx);
  60 int  mtx_lock(mtx_t *mtx);
  61 int  mtx_timedlock(mtx_t *mtx, const struct timespec *ts);
  62 int  mtx_trylock(mtx_t *mtx);
  63 int  mtx_unlock(mtx_t *mtx);
  64 
  65 void call_once(once_flag *flag, void (*fn)());
  66 
  67 int  cnd_init(cnd_t *cnd);
  68 void cnd_destroy(cnd_t *cnd);
  69 int  cnd_broadcast(cnd_t *cnd);
  70 int  cnd_signal(cnd_t *cnd);
  71 int  cnd_wait(cnd_t *cnd, mtx_t *mtx);
  72 int  cnd_timedwait(cnd_t *cnd, mtx_t *mtx, const struct timespec *ts);
  73 
  74 int   tss_create(tss_t *key, tss_dtor_t dtor);
  75 void  tss_delete(tss_t key);
  76 void *tss_get(tss_t key);
  77 int   tss_set(tss_t key, void *val);
  78 
  79 int    thrd_create(thrd_t *thr, thrd_start_t fn, void *arg);
  80 void   thrd_exit(int code);
  81 thrd_t thrd_current(void);
  82 int    thrd_detach(thrd_t thr);
  83 int    thrd_join(thrd_t thr, int *code);
  84 int    thrd_equal(thrd_t a, thrd_t b);
  85 void   thrd_sleep(const struct timespec *ts);
  86 void   thrd_yield(void);
  87 
  88 
  89 #endif 

/* [previous][next][first][last][top][bottom][index][help] [+89 threads.h] */