Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 1 | #region Copyright notice and license |
| 2 | |
Jan Tattermusch | af77b3d | 2015-02-13 11:22:21 -0800 | [diff] [blame] | 3 | // Copyright 2015, Google Inc. |
Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 4 | // All rights reserved. |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 5 | // |
Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 6 | // Redistribution and use in source and binary forms, with or without |
| 7 | // modification, are permitted provided that the following conditions are |
| 8 | // met: |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 9 | // |
Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 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. |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 19 | // |
Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 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 | |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 34 | using System; |
Jan Tattermusch | 3086862 | 2015-02-19 09:22:33 -0800 | [diff] [blame] | 35 | using System.Collections.Generic; |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 36 | using System.Runtime.InteropServices; |
| 37 | using System.Threading; |
| 38 | using System.Threading.Tasks; |
Jan Tattermusch | 3086862 | 2015-02-19 09:22:33 -0800 | [diff] [blame] | 39 | using Grpc.Core.Internal; |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 40 | |
Jan Tattermusch | 3086862 | 2015-02-19 09:22:33 -0800 | [diff] [blame] | 41 | namespace Grpc.Core.Internal |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 42 | { |
| 43 | /// <summary> |
| 44 | /// Pool of threads polling on the same completion queue. |
| 45 | /// </summary> |
| 46 | internal class GrpcThreadPool |
| 47 | { |
Jan Tattermusch | 04eb89c | 2015-06-12 13:03:05 -0700 | [diff] [blame] | 48 | readonly GrpcEnvironment environment; |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 49 | readonly object myLock = new object(); |
| 50 | readonly List<Thread> threads = new List<Thread>(); |
| 51 | readonly int poolSize; |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 52 | |
| 53 | CompletionQueueSafeHandle cq; |
| 54 | |
Jan Tattermusch | 04eb89c | 2015-06-12 13:03:05 -0700 | [diff] [blame] | 55 | public GrpcThreadPool(GrpcEnvironment environment, int poolSize) |
Jan Tattermusch | 075dde4 | 2015-03-11 18:21:00 -0700 | [diff] [blame] | 56 | { |
Jan Tattermusch | 04eb89c | 2015-06-12 13:03:05 -0700 | [diff] [blame] | 57 | this.environment = environment; |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 58 | this.poolSize = poolSize; |
| 59 | } |
| 60 | |
Jan Tattermusch | 075dde4 | 2015-03-11 18:21:00 -0700 | [diff] [blame] | 61 | public void Start() |
| 62 | { |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 63 | lock (myLock) |
| 64 | { |
| 65 | if (cq != null) |
| 66 | { |
| 67 | throw new InvalidOperationException("Already started."); |
| 68 | } |
| 69 | |
| 70 | cq = CompletionQueueSafeHandle.Create(); |
| 71 | |
| 72 | for (int i = 0; i < poolSize; i++) |
| 73 | { |
| 74 | threads.Add(CreateAndStartThread(i)); |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
Jan Tattermusch | 075dde4 | 2015-03-11 18:21:00 -0700 | [diff] [blame] | 79 | public void Stop() |
| 80 | { |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 81 | lock (myLock) |
| 82 | { |
| 83 | cq.Shutdown(); |
| 84 | |
Jan Tattermusch | 04eb89c | 2015-06-12 13:03:05 -0700 | [diff] [blame] | 85 | Console.WriteLine("Waiting for GRPC threads to finish."); |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 86 | foreach (var thread in threads) |
| 87 | { |
| 88 | thread.Join(); |
| 89 | } |
| 90 | |
| 91 | cq.Dispose(); |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | |
| 95 | internal CompletionQueueSafeHandle CompletionQueue |
| 96 | { |
| 97 | get |
| 98 | { |
| 99 | return cq; |
| 100 | } |
| 101 | } |
| 102 | |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 103 | private Thread CreateAndStartThread(int i) |
| 104 | { |
| 105 | var thread = new Thread(new ThreadStart(RunHandlerLoop)); |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 106 | thread.IsBackground = false; |
| 107 | thread.Start(); |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 108 | thread.Name = "grpc " + i; |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 109 | return thread; |
| 110 | } |
| 111 | |
| 112 | /// <summary> |
| 113 | /// Body of the polling thread. |
| 114 | /// </summary> |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 115 | private void RunHandlerLoop() |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 116 | { |
Jan Tattermusch | d367748 | 2015-06-01 19:27:40 -0700 | [diff] [blame] | 117 | CompletionQueueEvent ev; |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 118 | do |
| 119 | { |
Jan Tattermusch | d367748 | 2015-06-01 19:27:40 -0700 | [diff] [blame] | 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 | { |
Jan Tattermusch | 04eb89c | 2015-06-12 13:03:05 -0700 | [diff] [blame] | 127 | var callback = environment.CompletionRegistry.Extract(tag); |
Jan Tattermusch | d367748 | 2015-06-01 19:27:40 -0700 | [diff] [blame] | 128 | callback(success); |
| 129 | } |
| 130 | catch (Exception e) |
| 131 | { |
| 132 | Console.WriteLine("Exception occured while invoking completion delegate: " + e); |
| 133 | } |
| 134 | } |
Jan Tattermusch | 075dde4 | 2015-03-11 18:21:00 -0700 | [diff] [blame] | 135 | } |
Jan Tattermusch | d367748 | 2015-06-01 19:27:40 -0700 | [diff] [blame] | 136 | while (ev.type != GRPCCompletionType.Shutdown); |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 137 | Console.WriteLine("Completion queue has shutdown successfully, thread " + Thread.CurrentThread.Name + " exiting."); |
| 138 | } |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 139 | } |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 140 | } |