Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Functional] Adds occa::function and occa::array #442

Merged
merged 20 commits into from
Jan 17, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
[Utils] Split out mutex to its own file
  • Loading branch information
dmed256 committed Jan 17, 2021
commit bbbd0694d5cf62c7af86761fabdba97741116bf9
27 changes: 27 additions & 0 deletions include/occa/utils/mutex.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#ifndef OCCA_UTILS_MUTEX_HEADER
#define OCCA_UTILS_MUTEX_HEADER

#include <occa/defines.hpp>

#if (OCCA_OS & (OCCA_LINUX_OS | OCCA_MACOS_OS))
# include <pthread.h>
#endif

namespace occa {
class mutex_t {
public:
#if (OCCA_OS & (OCCA_LINUX_OS | OCCA_MACOS_OS))
pthread_mutex_t mutexHandle;
#else
void *mutexHandle;
#endif

mutex_t();
void free();

void lock();
void unlock();
};
}

#endif
48 changes: 2 additions & 46 deletions src/occa/internal/utils/sys.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ namespace occa {
}

bool isSafeToRmrf(const std::string &filename) {
const std::string expFilename = io::filename(filename);
const std::string expFilename = io::expandFilename(filename);
int depth = 0;

strVector path = split(expFilename, '/', '\0');
Expand Down Expand Up @@ -330,7 +330,7 @@ namespace occa {
}

void mkpath(const std::string &dir) {
strVector path = split(io::filename(dir), '/', '\0');
strVector path = split(io::expandFilename(dir), '/', '\0');

const int dirCount = (int) path.size();
std::string sPath;
Expand Down Expand Up @@ -1090,48 +1090,4 @@ namespace occa {
#endif
}
}

mutex::mutex() {
#if (OCCA_OS & (OCCA_LINUX_OS | OCCA_MACOS_OS))
int error = pthread_mutex_init(&mutexHandle, NULL);
#if OCCA_UNSAFE
ignoreResult(error);
#endif

OCCA_ERROR("Error initializing mutex",
error == 0);
#else
mutexHandle = CreateMutex(NULL, FALSE, NULL);
#endif
}

void mutex::free() {
#if (OCCA_OS & (OCCA_LINUX_OS | OCCA_MACOS_OS))
int error = pthread_mutex_destroy(&mutexHandle);
#if OCCA_UNSAFE
ignoreResult(error);
#endif

OCCA_ERROR("Error freeing mutex",
error == 0);
#else
CloseHandle(mutexHandle);
#endif
}

void mutex::lock() {
#if (OCCA_OS & (OCCA_LINUX_OS | OCCA_MACOS_OS))
pthread_mutex_lock(&mutexHandle);
#else
WaitForSingleObject(mutexHandle, INFINITE);
#endif
}

void mutex::unlock() {
#if (OCCA_OS & (OCCA_LINUX_OS | OCCA_MACOS_OS))
pthread_mutex_unlock(&mutexHandle);
#else
ReleaseMutex(mutexHandle);
#endif
}
}
15 changes: 0 additions & 15 deletions src/occa/internal/utils/sys.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,21 +171,6 @@ namespace occa {
std::string prettyStackSymbol(void *frame, const char *symbol);
//==================================
}

class mutex {
public:
#if (OCCA_OS & (OCCA_LINUX_OS | OCCA_MACOS_OS))
pthread_mutex_t mutexHandle;
#else
void *mutexHandle;
#endif

mutex();
void free();

void lock();
void unlock();
};
}

#endif
49 changes: 49 additions & 0 deletions src/utils/mutex.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include <occa/utils/mutex.hpp>
#include <occa/utils/logging.hpp>

namespace occa {
mutex_t::mutex_t() :
mutexHandle(PTHREAD_MUTEX_INITIALIZER) {
#if (OCCA_OS & (OCCA_LINUX_OS | OCCA_MACOS_OS))
int error = pthread_mutex_init(&mutexHandle, NULL);
#if OCCA_UNSAFE
ignoreResult(error);
#endif

OCCA_ERROR("Error initializing mutex",
error == 0);
#else
mutexHandle = CreateMutex(NULL, FALSE, NULL);
#endif
}

void mutex_t::free() {
#if (OCCA_OS & (OCCA_LINUX_OS | OCCA_MACOS_OS))
int error = pthread_mutex_destroy(&mutexHandle);
#if OCCA_UNSAFE
ignoreResult(error);
#endif

OCCA_ERROR("Error freeing mutex",
error == 0);
#else
CloseHandle(mutexHandle);
#endif
}

void mutex_t::lock() {
#if (OCCA_OS & (OCCA_LINUX_OS | OCCA_MACOS_OS))
pthread_mutex_lock(&mutexHandle);
#else
WaitForSingleObject(mutexHandle, INFINITE);
#endif
}

void mutex_t::unlock() {
#if (OCCA_OS & (OCCA_LINUX_OS | OCCA_MACOS_OS))
pthread_mutex_unlock(&mutexHandle);
#else
ReleaseMutex(mutexHandle);
#endif
}
}