src/thread.h
changeset 9479 c8ab793e4595
parent 9477 00be34b8e9fd
equal deleted inserted replaced
9478:d3fc1597603e 9479:c8ab793e4595
     4 
     4 
     5 #ifndef THREAD_H
     5 #ifndef THREAD_H
     6 #define THREAD_H
     6 #define THREAD_H
     7 
     7 
     8 typedef void (*OTTDThreadFunc)(void *);
     8 typedef void (*OTTDThreadFunc)(void *);
     9 typedef void (*OTTDThreadTerminateFunc)(class ThreadObject *self);
       
    10 
     9 
    11 /**
    10 /**
    12  * A Thread Object which works on all our supported OSes.
    11  * A Thread Object which works on all our supported OSes.
    13  */
    12  */
    14 class ThreadObject {
    13 class ThreadObject {
    55 	/**
    54 	/**
    56 	 * Create a thread; proc will be called as first function inside the thread,
    55 	 * Create a thread; proc will be called as first function inside the thread,
    57 	 *  with optinal params.
    56 	 *  with optinal params.
    58 	 * @param proc The procedure to call inside the thread.
    57 	 * @param proc The procedure to call inside the thread.
    59 	 * @param param The params to give with 'proc'.
    58 	 * @param param The params to give with 'proc'.
    60 	 * @param terminate_func The function (or NULL) to call when the thread terminates.
       
    61 	 * @return True if the thread was started correctly.
    59 	 * @return True if the thread was started correctly.
    62 	 */
    60 	 */
    63 	static ThreadObject *New(OTTDThreadFunc proc, void *param, OTTDThreadTerminateFunc terminate_func);
    61 	static ThreadObject *New(OTTDThreadFunc proc, void *param);
    64 
    62 
    65 	/**
    63 	/**
    66 	 * Convert the current thread to a new ThreadObject.
    64 	 * Convert the current thread to a new ThreadObject.
    67 	 * @return A new ThreadObject with the current thread attached to it.
    65 	 * @return A new ThreadObject with the current thread attached to it.
    68 	 */
    66 	 */
    71 	/**
    69 	/**
    72 	 * Find the Id of the current running thread.
    70 	 * Find the Id of the current running thread.
    73 	 * @return The thread ID of the current active thread.
    71 	 * @return The thread ID of the current active thread.
    74 	 */
    72 	 */
    75 	static uint CurrentId();
    73 	static uint CurrentId();
    76 
       
    77 	/**
       
    78 	 * A OTTDThreadTerminateFunc, which cleans up the thread itself
       
    79 	 *  at termination of the thread (so it becomes self-managed).
       
    80 	 */
       
    81 	static void TerminateCleanup(ThreadObject *self) { delete self; }
       
    82 };
    74 };
    83 
    75 
    84 /**
    76 /**
    85  * Cross-platform Thread Semaphore. Wait() waits for a Set() of someone else.
    77  * Cross-platform Thread Semaphore. Wait() waits for a Set() of someone else.
    86  */
    78  */