thread.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. (c) Copyright 2001-2009 The world wide DirectFB Open Source Community (directfb.org)
  3. (c) Copyright 2000-2004 Convergence (integrated media) GmbH
  4. All rights reserved.
  5. Written by Denis Oliver Kropp <dok@directfb.org>,
  6. Andreas Hundt <andi@fischlustig.de>,
  7. Sven Neumann <neo@directfb.org>,
  8. Ville Syrjälä <syrjala@sci.fi> and
  9. Claudio Ciccani <klan@users.sf.net>.
  10. This library is free software; you can redistribute it and/or
  11. modify it under the terms of the GNU Lesser General Public
  12. License as published by the Free Software Foundation; either
  13. version 2 of the License, or (at your option) any later version.
  14. This library is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. Lesser General Public License for more details.
  18. You should have received a copy of the GNU Lesser General Public
  19. License along with this library; if not, write to the
  20. Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  21. Boston, MA 02111-1307, USA.
  22. */
  23. #ifndef __DIRECT__THREAD_H__
  24. #define __DIRECT__THREAD_H__
  25. #include <sys/types.h>
  26. #include <direct/types.h>
  27. #include <direct/conf.h>
  28. #include <direct/mutex.h>
  29. typedef enum {
  30. DTT_DEFAULT = 0,
  31. DTT_CLEANUP = -5,
  32. DTT_INPUT = -10,
  33. DTT_OUTPUT = -12,
  34. DTT_MESSAGING = -15,
  35. DTT_CRITICAL = -20
  36. } DirectThreadType;
  37. typedef void * (*DirectThreadMainFunc)( DirectThread *thread, void *arg );
  38. typedef void (*DirectThreadInitFunc)( DirectThread *thread, void *arg );
  39. /*
  40. * Add a handler being called at the beginning of new threads.
  41. */
  42. DirectThreadInitHandler *direct_thread_add_init_handler ( DirectThreadInitFunc func,
  43. void *arg );
  44. /*
  45. * Remove the specified handler.
  46. */
  47. void direct_thread_remove_init_handler( DirectThreadInitHandler *handler );
  48. /*
  49. * Create a new thread and start it.
  50. * The thread type is relevant for the scheduling priority.
  51. */
  52. DirectThread *direct_thread_create ( DirectThreadType thread_type,
  53. DirectThreadMainFunc thread_main,
  54. void *arg,
  55. const char *name );
  56. /*
  57. * Returns the thread of the caller.
  58. */
  59. DirectThread *direct_thread_self ( void );
  60. /*
  61. * Returns the name of the specified thread.
  62. */
  63. const char *direct_thread_get_name ( DirectThread *thread );
  64. /*
  65. * Returns the thread ID of the specified thread.
  66. */
  67. pid_t direct_thread_get_tid ( DirectThread *thread );
  68. /*
  69. * Returns the name of the calling thread.
  70. */
  71. const char *direct_thread_self_name ( void );
  72. /*
  73. * Changes the name of the calling thread.
  74. */
  75. void direct_thread_set_name ( const char *name );
  76. /*
  77. * Wait on the thread object to be notified via direct_thread_notify().
  78. */
  79. DirectResult direct_thread_wait ( DirectThread *thread,
  80. int timeout_ms );
  81. /*
  82. * Notify the thread object waking up callers of direct_thread_wait().
  83. */
  84. void direct_thread_notify ( DirectThread *thread );
  85. void direct_thread_lock ( DirectThread *thread );
  86. void direct_thread_unlock ( DirectThread *thread );
  87. /*
  88. * Kindly ask the thread to terminate (for joining without thread cancellation).
  89. */
  90. void direct_thread_terminate ( DirectThread *thread );
  91. /*
  92. * Cancel a running thread.
  93. */
  94. void direct_thread_cancel ( DirectThread *thread );
  95. /*
  96. * Returns true if the specified thread has been canceled.
  97. */
  98. bool direct_thread_is_canceled( DirectThread *thread );
  99. /*
  100. * Detach a thread.
  101. */
  102. void direct_thread_detach ( DirectThread *thread );
  103. /*
  104. * Returns true if the specified thread has been detached.
  105. */
  106. bool direct_thread_is_detached( DirectThread *thread );
  107. /*
  108. * Check if the calling thread is canceled.
  109. * Must not be called by other threads than 'thread'.
  110. * This function won't return if the thread is canceled.
  111. */
  112. void direct_thread_testcancel ( DirectThread *thread );
  113. /*
  114. * Wait until a running thread is terminated.
  115. */
  116. void direct_thread_join ( DirectThread *thread );
  117. /*
  118. * Returns true if the specified thread has been join.
  119. */
  120. bool direct_thread_is_joined ( DirectThread *thread );
  121. /*
  122. * Free resources allocated by direct_thread_create.
  123. * If the thread is still running it will be killed.
  124. */
  125. void direct_thread_destroy ( DirectThread *thread );
  126. /*
  127. * Utilities for stringification.
  128. */
  129. #if DIRECT_BUILD_TEXT
  130. const char *direct_thread_type_name ( DirectThreadType type );
  131. const char *direct_thread_scheduler_name( DirectConfigThreadScheduler scheduler );
  132. const char *direct_thread_policy_name ( int policy );
  133. #endif
  134. #endif