src/thread_pthread.cpp
changeset 9476 902f9cf6373f
parent 9086 4887c2e470e4
child 9477 00be34b8e9fd
equal deleted inserted replaced
9475:cf1925ffac71 9476:902f9cf6373f
    93 
    93 
    94 		/* For now we terminate by throwing an error, gives much cleaner cleanup */
    94 		/* For now we terminate by throwing an error, gives much cleaner cleanup */
    95 		throw 0;
    95 		throw 0;
    96 	}
    96 	}
    97 
    97 
    98 	/* virtual */ void *Join()
    98 	/* virtual */ void Join()
    99 	{
    99 	{
   100 		/* You cannot join yourself */
   100 		/* You cannot join yourself */
   101 		assert(!IsCurrent());
   101 		assert(!IsCurrent());
   102 
   102 
   103 		void *ret;
   103 		pthread_join(m_thr, NULL);
   104 		pthread_join(m_thr, &ret);
       
   105 		m_thr = 0;
   104 		m_thr = 0;
   106 
   105 
   107 		delete this;
   106 		delete this;
   108 
       
   109 		return ret;
       
   110 	}
   107 	}
   111 
   108 
   112 	/* virtual */ bool IsCurrent()
   109 	/* virtual */ bool IsCurrent()
   113 	{
   110 	{
   114 		return pthread_self() == m_thr;
   111 		return pthread_self() == m_thr;
   124 	 * On thread creation, this function is called, which calls the real startup
   121 	 * On thread creation, this function is called, which calls the real startup
   125 	 *  function. This to get back into the correct instance again.
   122 	 *  function. This to get back into the correct instance again.
   126 	 */
   123 	 */
   127 	static void *stThreadProc(void *thr)
   124 	static void *stThreadProc(void *thr)
   128 	{
   125 	{
   129 		return ((ThreadObject_pthread *)thr)->ThreadProc();
   126 		((ThreadObject_pthread *)thr)->ThreadProc();
       
   127 		pthread_exit(NULL);
   130 	}
   128 	}
   131 
   129 
   132 	/**
   130 	/**
   133 	 * A new thread is created, and this function is called. Call the custom
   131 	 * A new thread is created, and this function is called. Call the custom
   134 	 *  function of the creator of the thread.
   132 	 *  function of the creator of the thread.
   135 	 */
   133 	 */
   136 	void *ThreadProc()
   134 	void ThreadProc()
   137 	{
   135 	{
   138 		/* The new thread stops here so the calling thread can complete pthread_create() call */
   136 		/* The new thread stops here so the calling thread can complete pthread_create() call */
   139 		sem_wait(&m_sem_start);
   137 		sem_wait(&m_sem_start);
   140 
   138 
   141 		/* Did this thread die naturally/via exit, or did it join? */
   139 		/* Did this thread die naturally/via exit, or did it join? */
   150 
   148 
   151 		/* Notify threads waiting for our completion */
   149 		/* Notify threads waiting for our completion */
   152 		sem_post(&m_sem_stop);
   150 		sem_post(&m_sem_stop);
   153 
   151 
   154 		if (exit) delete this;
   152 		if (exit) delete this;
   155 
       
   156 		pthread_exit(NULL);
       
   157 	}
   153 	}
   158 };
   154 };
   159 
   155 
   160 /* static */ ThreadObject *ThreadObject::New(OTTDThreadFunc proc, void *param)
   156 /* static */ ThreadObject *ThreadObject::New(OTTDThreadFunc proc, void *param)
   161 {
   157 {