thread.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*
  2. * thread.h
  3. *
  4. * Copyright (c) 2012 Martin Szulecki All Rights Reserved.
  5. * Copyright (c) 2012 Nikias Bassen All Rights Reserved.
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #ifndef __THREAD_H
  22. #define __THREAD_H
  23. #ifdef WIN32
  24. #include <windows.h>
  25. typedef HANDLE thread_t;
  26. typedef CRITICAL_SECTION mutex_t;
  27. typedef volatile struct {
  28. LONG lock;
  29. int state;
  30. } thread_once_t;
  31. #define THREAD_ONCE_INIT {0, 0}
  32. #define THREAD_ID GetCurrentThreadId()
  33. #else
  34. #include <pthread.h>
  35. typedef pthread_t thread_t;
  36. typedef pthread_mutex_t mutex_t;
  37. typedef pthread_once_t thread_once_t;
  38. #define THREAD_ONCE_INIT PTHREAD_ONCE_INIT
  39. #define THREAD_ID pthread_self()
  40. #endif
  41. typedef void* (*thread_func_t)(void* data);
  42. int thread_new(thread_t* thread, thread_func_t thread_func, void* data);
  43. void thread_free(thread_t thread);
  44. void thread_join(thread_t thread);
  45. void mutex_init(mutex_t* mutex);
  46. void mutex_destroy(mutex_t* mutex);
  47. void mutex_lock(mutex_t* mutex);
  48. void mutex_unlock(mutex_t* mutex);
  49. void thread_once(thread_once_t *once_control, void (*init_routine)(void));
  50. #endif