aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/csharp/GrpcCore/Internal/CompletionQueueSafeHandle.cs
blob: f098de682052fa8a69ba82e7b34057df97a02678 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using System;
using System.Runtime.InteropServices;
using System.Threading.Tasks;

namespace Google.GRPC.Core.Internal
{
    /// <summary>
    /// grpc_completion_queue from <grpc/grpc.h>
    /// </summary>
	internal class CompletionQueueSafeHandle : SafeHandleZeroIsInvalid
	{
        [DllImport("grpc.dll")]
        static extern CompletionQueueSafeHandle grpc_completion_queue_create();

        [DllImport("grpc.dll")]
        static extern EventSafeHandle grpc_completion_queue_pluck(CompletionQueueSafeHandle cq, IntPtr tag, Timespec deadline);

        [DllImport("grpc.dll")]
        static extern EventSafeHandle grpc_completion_queue_next(CompletionQueueSafeHandle cq, Timespec deadline);

        [DllImport("grpc.dll")]
        static extern void grpc_completion_queue_shutdown(CompletionQueueSafeHandle cq);

        [DllImport("grpc_csharp_ext.dll")]
        static extern GRPCCompletionType grpc_completion_queue_next_with_callback(CompletionQueueSafeHandle cq);

        [DllImport("grpc.dll")]
        static extern void grpc_completion_queue_destroy(IntPtr cq);

        private CompletionQueueSafeHandle()
        {
        }

        public static CompletionQueueSafeHandle Create()
        {
            return grpc_completion_queue_create();
        }

        public EventSafeHandle Next(Timespec deadline)
        {
            return grpc_completion_queue_next(this, deadline);
        }

        public GRPCCompletionType NextWithCallback()
        {
            return grpc_completion_queue_next_with_callback(this);
        }

        public EventSafeHandle Pluck(IntPtr tag, Timespec deadline)
        {
            return grpc_completion_queue_pluck(this, tag, deadline);
        }

        public void Shutdown()
        {
            grpc_completion_queue_shutdown(this);
        }

		protected override bool ReleaseHandle()
        {
            grpc_completion_queue_destroy(handle);
			return true;
		}
	}
}