1 // =================================
 2 // Copyright (c) 2024 Seppo Laakko
 3 // Distributed under the MIT license
 4 // =================================
 5 
 6 using System;
 7 
 8 public extern cdecl int RtmGetHardwareConcurrency();
 9 public extern cdecl int RtmStartThreadFunction(System.Threading.ThreadStartFunction function);
10 public extern cdecl int RtmStartThreadFunctionWithParam(System.Threading.ParameterizedThreadStartFunction functionvoid* param);
11 public extern cdecl int RtmStartThreadMethod(System.Threading.ThreadStartMethod method);
12 public extern cdecl int RtmStartThreadMethodWithParam(System.Threading.ParameterizedThreadStartMethod methodvoid* param);
13 public extern cdecl bool RtmJoinThread(int threadId);
14 public extern cdecl int RtmThisThreadId();
15 
16 namespace System.Threading
17 {
18     public delegate void ThreadStartFunction();
19     public delegate void ParameterizedThreadStartFunction(void* param);
20     public class delegate void ThreadStartMethod();
21     public class delegate void ParameterizedThreadStartMethod(void* param);
22     
23     public int HardwareConcurrency()
24     {
25         return RtmGetHardwareConcurrency();
26     }
27     
28     public class Thread
29     {
30         public static Thread StartFunction(ThreadStartFunction function)
31         {
32             Thread thread(RtmStartThreadFunction(function));
33             return thread;
34         }
35         public static Thread StartFunction(ParameterizedThreadStartFunction functionvoid* param)
36         {
37             Thread thread(RtmStartThreadFunctionWithParam(functionparam));
38             return thread;
39         }
40         public static Thread StartMethod(ThreadStartMethod method)
41         {
42             Thread thread(RtmStartThreadMethod(method));
43             return thread;
44         }
45         public static Thread StartMethod(ParameterizedThreadStartMethod methodvoid* param)
46         {
47             Thread thread(RtmStartThreadMethodWithParam(methodparam));
48             return thread;
49         }
50         public Thread() : id(0)
51         {
52         }
53         private Thread(int id_) : id(id_)
54         {
55         }
56         public ~Thread()
57         {
58             if (id != 0)
59             {
60                 Join();
61             }
62         }
63         suppress Thread(const Thread&);
64         suppress void operator=(const Thread&);
65         public Thread(Thread&& that) : id(that.id)
66         {
67             that.id = 0;
68         }
69         public default void operator=(Thread&&);
70         public inline int Id() const
71         {
72             return id;
73         }
74         public Result<bool> Join()
75         {
76             int jid = id;
77             id = 0;
78             if (!RtmJoinThread(jid))
79             {
80                 string errorMessage = "thread " + ToString(id) + " not joinable";
81                 int errorId = AllocateError(errorMessage);
82                 return Result<bool>(ErrorId(errorId));
83             }
84             return Result<bool>(true);
85         }
86         private int id;
87     }