1 #pragma once
2 
3 /** @file the_Foundation/thread.h  Thread object.
4 
5 @authors Copyright (c) 2017 Jaakko Keränen <jaakko.keranen@iki.fi>
6 
7 @par License
8 
9 Redistribution and use in source and binary forms, with or without
10 modification, are permitted provided that the following conditions are met:
11 
12 1. Redistributions of source code must retain the above copyright notice, this
13    list of conditions and the following disclaimer.
14 2. Redistributions in binary form must reproduce the above copyright notice,
15    this list of conditions and the following disclaimer in the documentation
16    and/or other materials provided with the distribution.
17 
18 <small>THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
22 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.</small>
28 */
29 
30 #include "defs.h"
31 #include "object.h"
32 #include "time.h"
33 #include "blockhash.h"
34 #include "mutex.h"
35 #include "audience.h"
36 #include "stdthreads.h"
37 #include "string.h"
38 
39 iBeginPublic
40 
41 iDeclareClass(Thread)
42 
43 typedef thrd_t          iThreadId;
44 typedef intptr_t        iThreadResult;
45 typedef iThreadResult (*iThreadRunFunc)(iThread *);
46 
47 iDeclareNotifyFunc(Thread, Finished)
48 
49 enum iThreadState {
50     created_ThreadState,
51     running_ThreadState,
52     finished_ThreadState,
53 };
54 
55 enum iThreadFlag {
56     terminationEnabled_ThreadFlag = 0x1,
57 };
58 
59 struct Impl_Thread {
60     iObject object;
61     iMutex mutex;
62     iString name;
63     iThreadId id;
64     iThreadRunFunc run;
65     iAtomicInt state; // enum iThreadState
66     uint32_t flags;
67     void *userData;
68     iThreadResult result;
69     iCondition finishedCond;
70     iAudience *finished;
71 };
72 
73 iDeclareObjectConstructionArgs(Thread, iThreadRunFunc run)
74 
75 void    setName_Thread              (iThread *, const char *name);
76 void    setUserData_Thread          (iThread *, void *userData);
77 void    setTerminationEnabled_Thread(iThread *, iBool enable);
78 
79 iBool           isRunning_Thread    (const iThread *);
80 iBool           isFinished_Thread   (const iThread *);
81 const iString * name_Thread         (const iThread *);
82 void *          userData_Thread     (const iThread *);
83 
84 /**
85  * Returns the result value of the thread. If the thread is still running, the method
86  * will block until the result is available. In other words, when the method exits,
87  * the thread will always be not running any more.
88  *
89  * @return Thread result value.
90  */
91 iThreadResult result_Thread (const iThread *);
92 
93 void        start_Thread    (iThread *);
94 void        join_Thread     (iThread *);
95 void        terminate_Thread(iThread *);
96 
97 #define guardJoin_Thread(thread, mutex) { \
98     iThread *thd = NULL; \
99     iGuardMutex((mutex), thd = ref_Object(thread)); \
100     if (thd) { \
101         join_Thread(thd); \
102         deref_Object(thd); \
103     } \
104 }
105 
106 void        sleep_Thread    (double seconds);
107 iThread *   current_Thread  (void);
108 iBool       isCurrent_Thread(const iThread *);
109 
110 /**
111  * Determines the number of CPU cores on the system.
112  */
113 int     idealConcurrentCount_Thread (void);
114 
id_Thread(const iThread * d)115 iLocalDef thrd_t id_Thread(const iThread *d) {
116     return d->id;
117 }
118 
state_Thread(const iThread * d)119 iLocalDef enum iThreadState state_Thread(const iThread *d) {
120     return (enum iThreadState) d->state;
121 }
122 
123 iDefineInlineAudienceGetter(Thread, finished)
124 
125 /** @cond */
126 iDeclareBlockHash(ThreadHash, ThreadId, Thread)
127 iDeclareLockableObject(ThreadHash)
128 /** @endcond */
129 
130 iEndPublic
131