"Fossies" - the Fresh Open Source Software Archive

Member "dmd2/src/druntime/import/core/sync/semaphore.di" (20 Nov 2020, 1912 Bytes) of package /linux/misc/dmd.2.094.2.linux.tar.xz:


As a special service "Fossies" has tried to format the requested text file into HTML format (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    1 // D import file generated from 'src/core/sync/semaphore.d'
    2 module core.sync.semaphore;
    3 public import core.sync.exception;
    4 public import core.time;
    5 version (OSX)
    6 {
    7 	version = Darwin;
    8 }
    9 else
   10 {
   11 	version (iOS)
   12 	{
   13 		version = Darwin;
   14 	}
   15 	else
   16 	{
   17 		version (TVOS)
   18 		{
   19 			version = Darwin;
   20 		}
   21 		else
   22 		{
   23 			version (WatchOS)
   24 			{
   25 				version = Darwin;
   26 			}
   27 		}
   28 	}
   29 }
   30 version (Windows)
   31 {
   32 	private import core.sys.windows.basetsd;
   33 	private import core.sys.windows.winbase;
   34 	private import core.sys.windows.windef;
   35 	private import core.sys.windows.winerror;
   36 }
   37 else
   38 {
   39 	version (Darwin)
   40 	{
   41 		private import core.sync.config;
   42 		private import core.stdc.errno;
   43 		private import core.sys.posix.time;
   44 		private import core.sys.darwin.mach.semaphore;
   45 	}
   46 	else
   47 	{
   48 		version (Posix)
   49 		{
   50 			private import core.sync.config;
   51 			private import core.stdc.errno;
   52 			private import core.sys.posix.pthread;
   53 			private import core.sys.posix.semaphore;
   54 		}
   55 		else
   56 		{
   57 			static assert(false, "Platform not supported");
   58 		}
   59 	}
   60 }
   61 class Semaphore
   62 {
   63 	this(uint count = 0)
   64 	{
   65 		version (Windows)
   66 		{
   67 			m_hndl = CreateSemaphoreA(null, count, (int).max, null);
   68 			if (m_hndl == m_hndl.init)
   69 				throw new SyncError("Unable to create semaphore");
   70 		}
   71 		else
   72 		{
   73 			version (Darwin)
   74 			{
   75 				auto rc = semaphore_create(mach_task_self(), &m_hndl, SYNC_POLICY_FIFO, count);
   76 				if (rc)
   77 					throw new SyncError("Unable to create semaphore");
   78 			}
   79 			else
   80 			{
   81 				version (Posix)
   82 				{
   83 					int rc = sem_init(&m_hndl, 0, count);
   84 					if (rc)
   85 						throw new SyncError("Unable to create semaphore");
   86 				}
   87 
   88 			}
   89 		}
   90 	}
   91 	~this();
   92 	void wait();
   93 	bool wait(Duration period);
   94 	void notify();
   95 	bool tryWait();
   96 	protected 
   97 	{
   98 		version (Windows)
   99 		{
  100 			alias Handle = HANDLE;
  101 		}
  102 		else
  103 		{
  104 			version (Darwin)
  105 			{
  106 				alias Handle = semaphore_t;
  107 			}
  108 			else
  109 			{
  110 				version (Posix)
  111 				{
  112 					alias Handle = sem_t;
  113 				}
  114 			}
  115 		}
  116 		Handle m_hndl;
  117 	}
  118 }