Skip to content
Permalink
Browse files

nano: commit missing files excluded by gitignore

  • Loading branch information...
RMerl committed Oct 9, 2019
1 parent b424611 commit aa6597004e3e4ded8cf6a87b87ccd5345f7016e3
@@ -0,0 +1,35 @@
/* Init guards, somewhat like spinlocks (native Windows implementation).
Copyright (C) 2005-2019 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, see <https://www.gnu.org/licenses/>. */

/* Written by Bruno Haible <bruno@clisp.org>, 2005.
Based on GCC's gthr-win32.h. */

#ifndef _WINDOWS_INITGUARD_H
#define _WINDOWS_INITGUARD_H

#define WIN32_LEAN_AND_MEAN /* avoid including junk */
#include <windows.h>

typedef struct
{
volatile int done;
volatile LONG started;
}
glwthread_initguard_t;

#define GLWTHREAD_INITGUARD_INIT { 0, -1 }

#endif /* _WINDOWS_INITGUARD_H */
@@ -0,0 +1,95 @@
/* Plain mutexes (native Windows implementation).
Copyright (C) 2005-2019 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, see <https://www.gnu.org/licenses/>. */

/* Written by Bruno Haible <bruno@clisp.org>, 2005.
Based on GCC's gthr-win32.h. */

#include <config.h>

/* Specification. */
#include "windows-mutex.h"

#include <errno.h>

void
glwthread_mutex_init (glwthread_mutex_t *mutex)
{
InitializeCriticalSection (&mutex->lock);
mutex->guard.done = 1;
}

int
glwthread_mutex_lock (glwthread_mutex_t *mutex)
{
if (!mutex->guard.done)
{
if (InterlockedIncrement (&mutex->guard.started) == 0)
/* This thread is the first one to need this mutex. Initialize it. */
glwthread_mutex_init (mutex);
else
{
/* Don't let mutex->guard.started grow and wrap around. */
InterlockedDecrement (&mutex->guard.started);
/* Yield the CPU while waiting for another thread to finish
initializing this mutex. */
while (!mutex->guard.done)
Sleep (0);
}
}
EnterCriticalSection (&mutex->lock);
return 0;
}

int
glwthread_mutex_trylock (glwthread_mutex_t *mutex)
{
if (!mutex->guard.done)
{
if (InterlockedIncrement (&mutex->guard.started) == 0)
/* This thread is the first one to need this mutex. Initialize it. */
glwthread_mutex_init (mutex);
else
{
/* Don't let mutex->guard.started grow and wrap around. */
InterlockedDecrement (&mutex->guard.started);
/* Let another thread finish initializing this mutex, and let it also
lock this mutex. */
return EBUSY;
}
}
if (!TryEnterCriticalSection (&mutex->lock))
return EBUSY;
return 0;
}

int
glwthread_mutex_unlock (glwthread_mutex_t *mutex)
{
if (!mutex->guard.done)
return EINVAL;
LeaveCriticalSection (&mutex->lock);
return 0;
}

int
glwthread_mutex_destroy (glwthread_mutex_t *mutex)
{
if (!mutex->guard.done)
return EINVAL;
DeleteCriticalSection (&mutex->lock);
mutex->guard.done = 0;
return 0;
}
@@ -0,0 +1,51 @@
/* Plain mutexes (native Windows implementation).
Copyright (C) 2005-2019 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, see <https://www.gnu.org/licenses/>. */

/* Written by Bruno Haible <bruno@clisp.org>, 2005.
Based on GCC's gthr-win32.h. */

#ifndef _WINDOWS_MUTEX_H
#define _WINDOWS_MUTEX_H

#define WIN32_LEAN_AND_MEAN /* avoid including junk */
#include <windows.h>

#include "windows-initguard.h"

typedef struct
{
glwthread_initguard_t guard; /* protects the initialization */
CRITICAL_SECTION lock;
}
glwthread_mutex_t;

#define GLWTHREAD_MUTEX_INIT { GLWTHREAD_INITGUARD_INIT }

#ifdef __cplusplus
extern "C" {
#endif

extern void glwthread_mutex_init (glwthread_mutex_t *mutex);
extern int glwthread_mutex_lock (glwthread_mutex_t *mutex);
extern int glwthread_mutex_trylock (glwthread_mutex_t *mutex);
extern int glwthread_mutex_unlock (glwthread_mutex_t *mutex);
extern int glwthread_mutex_destroy (glwthread_mutex_t *mutex);

#ifdef __cplusplus
}
#endif

#endif /* _WINDOWS_MUTEX_H */
@@ -0,0 +1,62 @@
/* Once-only control (native Windows implementation).
Copyright (C) 2005-2019 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, see <https://www.gnu.org/licenses/>. */

/* Written by Bruno Haible <bruno@clisp.org>, 2005.
Based on GCC's gthr-win32.h. */

#include <config.h>

/* Specification. */
#include "windows-once.h"

#include <stdlib.h>

void
glwthread_once (glwthread_once_t *once_control, void (*initfunction) (void))
{
if (once_control->inited <= 0)
{
if (InterlockedIncrement (&once_control->started) == 0)
{
/* This thread is the first one to come to this once_control. */
InitializeCriticalSection (&once_control->lock);
EnterCriticalSection (&once_control->lock);
once_control->inited = 0;
initfunction ();
once_control->inited = 1;
LeaveCriticalSection (&once_control->lock);
}
else
{
/* Don't let once_control->started grow and wrap around. */
InterlockedDecrement (&once_control->started);
/* Some other thread has already started the initialization.
Yield the CPU while waiting for the other thread to finish
initializing and taking the lock. */
while (once_control->inited < 0)
Sleep (0);
if (once_control->inited <= 0)
{
/* Take the lock. This blocks until the other thread has
finished calling the initfunction. */
EnterCriticalSection (&once_control->lock);
LeaveCriticalSection (&once_control->lock);
if (!(once_control->inited > 0))
abort ();
}
}
}
}
@@ -0,0 +1,47 @@
/* Once-only control (native Windows implementation).
Copyright (C) 2005-2019 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, see <https://www.gnu.org/licenses/>. */

/* Written by Bruno Haible <bruno@clisp.org>, 2005.
Based on GCC's gthr-win32.h. */

#ifndef _WINDOWS_ONCE_H
#define _WINDOWS_ONCE_H

#define WIN32_LEAN_AND_MEAN /* avoid including junk */
#include <windows.h>

typedef struct
{
volatile int inited;
volatile LONG started;
CRITICAL_SECTION lock;
}
glwthread_once_t;

#define GLWTHREAD_ONCE_INIT { -1, -1 }

#ifdef __cplusplus
extern "C" {
#endif

extern void glwthread_once (glwthread_once_t *once_control,
void (*initfunction) (void));

#ifdef __cplusplus
}
#endif

#endif /* _WINDOWS_ONCE_H */

0 comments on commit aa65970

Please sign in to comment.
You can’t perform that action at this time.