Assists music production by grouping standalone programs into sessions. Community version of "Non Session Manager".
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

119 lines
2.8KB

  1. /*******************************************************************************/
  2. /* Copyright (C) 2008 Jonathan Moore Liles */
  3. /* */
  4. /* This program is free software; you can redistribute it and/or modify it */
  5. /* under the terms of the GNU General Public License as published by the */
  6. /* Free Software Foundation; either version 2 of the License, or (at your */
  7. /* option) any later version. */
  8. /* */
  9. /* This program is distributed in the hope that it will be useful, but WITHOUT */
  10. /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
  11. /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for */
  12. /* more details. */
  13. /* */
  14. /* You should have received a copy of the GNU General Public License along */
  15. /* with This program; see the file COPYING. If not,write to the Free Software */
  16. /* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  17. /*******************************************************************************/
  18. #include "Thread.H"
  19. #include <assert.h>
  20. #include <string.h>
  21. pthread_key_t Thread::_current = 0;
  22. Thread::Thread ( )
  23. {
  24. _thread = 0;
  25. _name = 0;
  26. }
  27. Thread::Thread ( const char *name )
  28. {
  29. _thread = 0;
  30. _name = name;
  31. }
  32. void
  33. Thread::init ( void )
  34. {
  35. pthread_key_create( &_current, NULL );
  36. }
  37. bool
  38. Thread::is ( const char *name )
  39. {
  40. return ! strcmp( Thread::current()->name(), name );
  41. }
  42. /** to be used by existing threads (that won't call clone()) */
  43. void
  44. Thread::set ( const char *name )
  45. {
  46. _thread = pthread_self();
  47. _name = name;
  48. pthread_setspecific( _current, (void*)this );
  49. }
  50. Thread *
  51. Thread::current ( void )
  52. {
  53. return (Thread*)pthread_getspecific( _current );
  54. }
  55. struct thread_data
  56. {
  57. void *(*entry_point)(void *);
  58. void *arg;
  59. void *t;
  60. };
  61. void *
  62. Thread::run_thread ( void *arg )
  63. {
  64. thread_data td = *(thread_data *)arg;
  65. delete (thread_data*)arg;
  66. pthread_setspecific( _current, td.t );
  67. return td.entry_point( td.arg );
  68. }
  69. bool
  70. Thread::clone ( void *(*entry_point)(void *), void *arg )
  71. {
  72. assert( ! _thread );
  73. thread_data *td = new thread_data;
  74. td->entry_point = entry_point;
  75. td->arg = arg;
  76. td->t = this;
  77. if ( pthread_create( &_thread, NULL, run_thread, td ) != 0 )
  78. return false;
  79. return true;
  80. }
  81. void
  82. Thread::detach ( void )
  83. {
  84. pthread_detach( _thread );
  85. _thread = 0;
  86. }
  87. void
  88. Thread::join ( void )
  89. {
  90. pthread_join( _thread, NULL );
  91. _thread = 0;
  92. }