public WorkerQueuePrivate(ThreadProc thread_proc, int num_worker_threads, object[] tasks) { thread_list = new List <ThreadObj>(); int i; this.thread_proc = thread_proc; this.num_worker_threads = num_worker_threads; foreach (object task in tasks) { taskQueue.Enqueue(task); } raised_exception = null; for (i = 0; i < num_worker_threads; i++) { ThreadObj t = new ThreadObj(worker_thread); thread_list.Add(t); } foreach (ThreadObj t in thread_list) { t.WaitForEnd(); } if (raised_exception != null) { throw raised_exception; } }
void tcpAcceptedThread(object param) { Sock s = (Sock)param; ThreadObj.NoticeInited(); this.acceptProc(this, s, this.acceptParam); }
public Cfg(bool read_only = true, int read_polling_interval_secs = 2, int update_polling_interval_secs = 1, T default_config = null, string filename = null, string header_str = null) { if (default_config == null) { default_config = new T(); } this.DefaultConfig = (T)default_config.CloneSerializableObject(); if (filename.IsEmpty()) { filename = "@" + Str.GetLastToken(default_config.GetType().ToString(), '+', '.').MakeSafeFileName() + ".cfg"; } this.FileName = IO.InnerFilePath(filename); this.DirName = this.FileName.GetDirectoryName(); IO.MakeDirIfNotExists(this.DirName); if (header_str.IsEmpty()) { header_str = @"# Configuration file # YAML format"; } this.HeaderStr = header_str; this.ReadOnly = read_only; if (IO.IsFileExists(this.FileName) == false) { WriteConfigToFile(this.FileName, this.DefaultConfig, this.HeaderStr); this.Config = this.DefaultConfig; } // 初期状態の読み込み (エラー発生時は例外を出す) T t = ReadConfigFromFile(filename, null); if (t == null) { // ファイル内容が空の場合はデフォルト Config を使用 t = this.DefaultConfig; WriteConfigToFile(this.FileName, this.DefaultConfig, this.HeaderStr); } this.Config = t; this.ReadPollingIntervalSecs = read_polling_interval_secs; this.UpdatePollingIntervalSecs = update_polling_interval_secs; current_version++; // スレッドの作成 halt_event = new Event(); polling_thread_obj = new ThreadObj(polling_thread); }
// 初期化 void init(int port, AcceptProc acceptProc, object acceptParam, bool localOnly, bool getHostName) { this.lockObj = new object(); this.port = port; this.acceptProc = acceptProc; this.acceptParam = acceptParam; this.status = ListenerStatus.Trying; this.eventObj = new Event(); this.halt = false; this.localOnly = localOnly; this.getHostName = getHostName; this.listenRetryTime = ListenRetryTimeDefault; // スレッドの作成 ThreadObj thread = new ThreadObj(new ThreadProc(ListenerThread)); thread.WaitForInit(); }
public BatchQueueItem <T> Add(T item) { BatchQueueItem <T> q = new BatchQueueItem <T>(item); lock (lockobj) { this.queue.Enqueue(q); if (thread_mode == 0) { thread_mode = 1; ThreadObj t = new ThreadObj(bg_thread_proc); } } new_event_signal.Set(); return(q); }
public static unsafe void Poll(IntPtr[] reads, IntPtr[] writes, int timeout) { if (timeout == 0) { return; } PollEvent[] p; int num, n, num_read_total, num_write_total; num_read_total = num_write_total = 0; foreach (IntPtr fd in reads) { if ((int)fd != -1) { num_read_total++; } } foreach (IntPtr fd in writes) { if ((int)fd != -1) { num_write_total++; } } num = num_read_total + num_write_total; p = new PollEvent[num]; n = 0; foreach (IntPtr fd in reads) { if ((int)fd != -1) { p[n].FileDescriptor = (int)fd; p[n].Events = PollEvents.POLLIN | PollEvents.POLLPRI | PollEvents.POLLERR | PollEvents.POLLHUP; n++; } } foreach (IntPtr fd in writes) { if ((int)fd != -1) { p[n].FileDescriptor = (int)fd; p[n].Events = PollEvents.POLLIN | PollEvents.POLLPRI | PollEvents.POLLERR | PollEvents.POLLHUP | PollEvents.POLLOUT; n++; } } if (num == 0) { ThreadObj.Sleep(timeout); } else { //Dbg.WriteLine("Poll Begin."); int ret = Poll(p, timeout); //Dbg.WriteLine($"Poll end: ret = {ret}, reads = {reads.Length}, writes = {writes.Length}, pfds = {p.Length}"); //for (int i = 0; i < reads.Length; i++) Dbg.WriteLine($"reads[{i}] = {reads[i]}"); //for (int i = 0; i < writes.Length; i++) Dbg.WriteLine($"writes[{i}] = {writes[i]}"); } }
// スリープ public static void SleepThread(int millisec) { ThreadObj.Sleep(millisec); }
public ChildProcess(string exe, string args = "", string input = "", bool throw_exception_on_exit_error = false, int timeout = ThreadObj.Infinite) { this.timeout = timeout; Str.NormalizeString(ref args); ProcessStartInfo info = new ProcessStartInfo() { FileName = IO.InnerFilePath(exe), Arguments = args, UseShellExecute = false, RedirectStandardOutput = true, RedirectStandardError = true, RedirectStandardInput = !Str.IsEmptyStr(input), }; ThreadObj t = null; using (Process p = Process.Start(info)) { this.proc = p; if (timeout != ThreadObj.Infinite) { timeout_thread_event = new Event(); t = new ThreadObj(timeout_thread); } if (Str.IsEmptyStr(input) == false) { p.StandardInput.Write(input); p.StandardInput.Flush(); p.StandardInput.Close(); } stdout = p.StandardOutput.ReadToEnd(); stderr = p.StandardError.ReadToEnd(); p.WaitForExit(); finished = true; if (timeout_thread_event != null) { timeout_thread_event.Set(); } if (t != null) { t.WaitForEnd(); } if (killed) { if (Str.IsEmptyStr(stderr)) { stderr = $"Process run timeout ({timeout.ToStr3()} msecs)."; } } exitcode = p.ExitCode; if (throw_exception_on_exit_error) { if (exitcode != 0) { throw new ApplicationException($"ChildProcess: '{exe}': exitcode = {exitcode}, errorstr = {stderr.OneLine()}"); } } } }
// スレッド public void ListenerThread(object param) { Sock new_sock, s; int num_failed; this.thread = ThreadObj.GetCurrentThreadObj(); this.status = ListenerStatus.Trying; ThreadObj.NoticeInited(); while (true) { bool firstFailed = true; this.status = ListenerStatus.Trying; // Listen を試みる while (true) { if (this.halt) { return; } try { s = Sock.Listen(this.port, this.localOnly); this.sock = s; break; } catch { if (firstFailed) { firstFailed = false; } this.eventObj.Wait(this.listenRetryTime); if (this.halt) { return; } } } this.status = ListenerStatus.Listening; if (this.halt) { this.sock.Disconnect(); break; } num_failed = 0; // Accept ループ while (true) { // Accept する new_sock = this.sock.Accept(this.getHostName); if (new_sock != null) { // 成功 tcpAccepted(new_sock); } else { // 失敗 if (this.halt == false) { if ((++num_failed) <= 5) { continue; } } this.sock.Disconnect(); break; } } if (this.halt) { return; } } }
// TCP 受付完了 void tcpAccepted(Sock s) { ThreadObj t = new ThreadObj(new ThreadProc(tcpAcceptedThread), s); t.WaitForInit(); }