Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(222)

Side by Side Diff: third_party/grpc/src/csharp/Grpc.Core/Internal/GrpcThreadPool.cs

Issue 1932353002: Initial checkin of gRPC to third_party/ Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 #region Copyright notice and license
2
3 // Copyright 2015, Google Inc.
4 // All rights reserved.
5 //
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are
8 // met:
9 //
10 // * Redistributions of source code must retain the above copyright
11 // notice, this list of conditions and the following disclaimer.
12 // * Redistributions in binary form must reproduce the above
13 // copyright notice, this list of conditions and the following disclaimer
14 // in the documentation and/or other materials provided with the
15 // distribution.
16 // * Neither the name of Google Inc. nor the names of its
17 // contributors may be used to endorse or promote products derived from
18 // this software without specific prior written permission.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
32 #endregion
33
34 using System;
35 using System.Collections.Generic;
36 using System.Runtime.InteropServices;
37 using System.Threading;
38 using System.Threading.Tasks;
39 using Grpc.Core.Logging;
40
41 namespace Grpc.Core.Internal
42 {
43 /// <summary>
44 /// Pool of threads polling on the same completion queue.
45 /// </summary>
46 internal class GrpcThreadPool
47 {
48 static readonly ILogger Logger = GrpcEnvironment.Logger.ForType<GrpcThre adPool>();
49
50 readonly GrpcEnvironment environment;
51 readonly object myLock = new object();
52 readonly List<Thread> threads = new List<Thread>();
53 readonly int poolSize;
54
55 CompletionQueueSafeHandle cq;
56
57 public GrpcThreadPool(GrpcEnvironment environment, int poolSize)
58 {
59 this.environment = environment;
60 this.poolSize = poolSize;
61 }
62
63 public void Start()
64 {
65 lock (myLock)
66 {
67 if (cq != null)
68 {
69 throw new InvalidOperationException("Already started.");
70 }
71
72 cq = CompletionQueueSafeHandle.Create();
73
74 for (int i = 0; i < poolSize; i++)
75 {
76 threads.Add(CreateAndStartThread(i));
77 }
78 }
79 }
80
81 public void Stop()
82 {
83 lock (myLock)
84 {
85 cq.Shutdown();
86 foreach (var thread in threads)
87 {
88 thread.Join();
89 }
90
91 cq.Dispose();
92 }
93 }
94
95 internal CompletionQueueSafeHandle CompletionQueue
96 {
97 get
98 {
99 return cq;
100 }
101 }
102
103 private Thread CreateAndStartThread(int i)
104 {
105 var thread = new Thread(new ThreadStart(RunHandlerLoop));
106 thread.IsBackground = false;
107 thread.Start();
108 thread.Name = "grpc " + i;
109 return thread;
110 }
111
112 /// <summary>
113 /// Body of the polling thread.
114 /// </summary>
115 private void RunHandlerLoop()
116 {
117 CompletionQueueEvent ev;
118 do
119 {
120 ev = cq.Next();
121 if (ev.type == GRPCCompletionType.OpComplete)
122 {
123 bool success = (ev.success != 0);
124 IntPtr tag = ev.tag;
125 try
126 {
127 var callback = environment.CompletionRegistry.Extract(ta g);
128 callback(success);
129 }
130 catch (Exception e)
131 {
132 Logger.Error(e, "Exception occured while invoking comple tion delegate");
133 }
134 }
135 }
136 while (ev.type != GRPCCompletionType.Shutdown);
137 }
138 }
139 }
OLDNEW
« no previous file with comments | « third_party/grpc/src/csharp/Grpc.Core/Internal/Enums.cs ('k') | third_party/grpc/src/csharp/Grpc.Core/Internal/INativeCall.cs » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698