src/thread_pthread.cpp
branchnoai
changeset 9859 81621c6ba0e9
parent 9857 7adb6a846add
child 10171 d4397d599d78
equal deleted inserted replaced
9858:b385c3ee0926 9859:81621c6ba0e9
   115 			return true;
   115 			return true;
   116 		}
   116 		}
   117 		return false;
   117 		return false;
   118 	}
   118 	}
   119 
   119 
       
   120 	/* virtual */ bool Exit()
       
   121 	{
       
   122 		/* You can only exit yourself */
       
   123 		assert(IsCurrent());
       
   124 		/* If the thread is not running, we are already closed */
       
   125 		if (!IsRunning()) return false;
       
   126 
       
   127 		/* For now we terminate by throwing an error, gives much cleaner cleanup */
       
   128 		throw 0;
       
   129 	}
       
   130 
       
   131 	/* virtual */ void Join()
       
   132 	{
       
   133 		/* You cannot join yourself */
       
   134 		assert(!IsCurrent());
       
   135 
       
   136 		pthread_join(m_thr, NULL);
       
   137 		m_thr = 0;
       
   138 	}
       
   139 
   120 	/* virtual */ bool IsCurrent()
   140 	/* virtual */ bool IsCurrent()
   121 	{
   141 	{
   122 		return pthread_self() == m_thr;
   142 		return pthread_self() == m_thr;
   123 	}
   143 	}
   124 
   144 
   132 	 * On thread creation, this function is called, which calls the real startup
   152 	 * On thread creation, this function is called, which calls the real startup
   133 	 *  function. This to get back into the correct instance again.
   153 	 *  function. This to get back into the correct instance again.
   134 	 */
   154 	 */
   135 	static void *stThreadProc(void *thr)
   155 	static void *stThreadProc(void *thr)
   136 	{
   156 	{
   137 		return ((ThreadObject_pthread*)thr)->ThreadProc();
   157 		return ((ThreadObject_pthread *)thr)->ThreadProc();
   138 	}
   158 	}
   139 
   159 
   140 	/**
   160 	/**
   141 	 * A new thread is created, and this function is called. Call the custom
   161 	 * A new thread is created, and this function is called. Call the custom
   142 	 *  function of the creator of the thread.
   162 	 *  function of the creator of the thread.
   148 
   168 
   149 		/* Call the proc of the creator to continue this thread */
   169 		/* Call the proc of the creator to continue this thread */
   150 		try {
   170 		try {
   151 			m_proc(m_param);
   171 			m_proc(m_param);
   152 		} catch (...) {
   172 		} catch (...) {
   153 			DEBUG(misc, 0, "Exception in thread %u!", GetId());
       
   154 		}
   173 		}
   155 		/* The thread died, so we are no longer valid */
       
   156 		m_thr = 0;
       
   157 
   174 
   158 		/* Notify threads waiting for our completion */
   175 		/* Notify threads waiting for our completion */
   159 		sem_post(&m_sem_stop);
   176 		sem_post(&m_sem_stop);
       
   177 
   160 		return NULL;
   178 		return NULL;
   161 	}
   179 	}
   162 };
   180 };
   163 
   181 
   164 /* static */ ThreadObject *ThreadObject::New(OTTDThreadFunc proc, void *param)
   182 /* static */ ThreadObject *ThreadObject::New(OTTDThreadFunc proc, void *param)