PageRenderTime 42ms CodeModel.GetById 33ms app.highlight 4ms RepoModel.GetById 2ms app.codeStats 0ms

/sparrowhawk/test_framework/ESTFThread.h

http://github.com/jtblatt/duderino
C Header | 97 lines | 31 code | 18 blank | 48 comment | 0 complexity | 3fabe7c8c756bd3c391430126469f2bb MD5 | raw file
 1/** @file ESTFThread.h
 2 *  @brief A platform-independent thread abstraction
 3 *
 4 * Copyright (c) 2009 Yahoo! Inc.
 5 * The copyrights embodied in the content of this file are licensed by Yahoo! Inc.
 6 * under the BSD (revised) open source license.
 7 *
 8 * Derived from code that is Copyright (c) 2009 Joshua Blatt and offered under both
 9 * BSD and Apache 2.0 licenses (http://sourceforge.net/projects/sparrowhawk/).
10 *
11 *  $Author: blattj $
12 *  $Date: 2009/05/25 21:51:19 $
13 *  $Name:  $
14 *  $Revision: 1.3 $
15 */
16
17#ifndef ESTF_THREAD_H
18#define ESTF_THREAD_H
19
20#ifndef ESTF_CONFIG_H
21#include <ESTFConfig.h>
22#endif
23
24#ifdef HAVE_PTHREAD_H
25#include <pthread.h>
26#endif
27
28/** ESTFThread should be subclassed by any class that wants to run in its own
29 *  thread of control.  Subclasses must define a run template method that this
30 *  base class will call in a new thread of control.
31 *
32 *  @ingroup test
33 */
34class ESTFThread
35{
36public:
37#ifdef HAVE_PTHREAD_T
38    typedef pthread_t ThreadId;
39#else
40#error "thread type required"
41#endif
42
43    /** Default constructor. */
44    ESTFThread();
45
46    /** Default destructor. */
47    virtual ~ESTFThread();
48
49    /** Spawns a new thread that will immediately call the run method.
50     *
51     *    @return true if new thread was spawned, false otherwise.
52     */
53    bool spawn();
54
55    /** Blocks the calling thread until this thread finishes execution.
56     *
57     *    @return true if join was successful, false otherwise.
58     */
59    bool join();
60
61    /** Get the thread id of this thread.
62     *
63      *    @return the thread id of this thread.
64     */
65    ThreadId getThreadId();
66
67    /** Get the current thread id of the calling thread.
68     *
69     *    @return the thread id of the calling thread.
70     */
71    static ThreadId GetThreadId();
72
73    /** Yield the processor to another thread or process.
74     */
75    static void Yield();
76
77    /** Put the calling thread to sleep
78     *
79     *  @param msec The number of milliseconds to sleep
80     */
81    static void Sleep( long msec );
82
83protected:
84
85    /** This is the main function for the new thread.  Subclasses must define
86     *    this.
87     */
88    virtual void run() = 0;
89
90private:
91
92    static void *ThreadEntry( void * arg );
93
94    ThreadId _threadId;
95};
96
97#endif /* ! ESTF_THREAD_H */