-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[umf] add windows support to critnib
Implement locks using C++ std::mutex and atomics by using Interlocked* functions and calling _ReadWriteBarrier
- Loading branch information
Showing
5 changed files
with
132 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
source/common/unified_malloc_framework/src/utils/utils_posix.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* | ||
* Copyright (C) 2023 Intel Corporation | ||
* | ||
* Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM Exceptions. | ||
* See LICENSE.TXT | ||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
* | ||
*/ | ||
|
||
#include <pthread.h> | ||
#include <stdlib.h> | ||
|
||
struct os_mutex_t *util_mutex_create() { | ||
pthread_mutex_t *mutex = (pthread_mutex_t *)malloc(sizeof(pthread_mutex_t)); | ||
int ret = pthread_mutex_init(mutex, NULL); | ||
return ret == 0 ? ((struct os_mutex_t *)mutex) : NULL; | ||
} | ||
|
||
void util_mutex_destroy(struct os_mutex_t *m) { | ||
pthread_mutex_t *mutex = (pthread_mutex_t *)m; | ||
int ret = pthread_mutex_destroy(mutex); | ||
(void)ret; // TODO: add logging | ||
free(m); | ||
} | ||
|
||
int util_mutex_lock(struct os_mutex_t *m) { | ||
return pthread_mutex_lock((pthread_mutex_t *)m); | ||
} | ||
|
||
int util_mutex_unlock(struct os_mutex_t *m) { | ||
return pthread_mutex_unlock((pthread_mutex_t *)m); | ||
} |
31 changes: 31 additions & 0 deletions
31
source/common/unified_malloc_framework/src/utils/utils_windows.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* | ||
* Copyright (C) 2023 Intel Corporation | ||
* | ||
* Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM Exceptions. | ||
* See LICENSE.TXT | ||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
* | ||
*/ | ||
|
||
#include <mutex> | ||
|
||
struct os_mutex_t *util_mutex_create(void) { | ||
return reinterpret_cast<struct os_mutex_t *>(new std::mutex); | ||
} | ||
|
||
void util_mutex_destroy(struct os_mutex_t *mutex) { | ||
delete reinterpret_cast<std::mutex *>(mutex); | ||
} | ||
|
||
int util_mutex_lock(struct os_mutex_t *mutex) try { | ||
reinterpret_cast<std::mutex *>(mutex)->lock(); | ||
return 0; | ||
} catch (std::system_error &err) { | ||
return err.code().value(); | ||
} | ||
|
||
int util_mutex_unlock(struct os_mutex_t *mutex) { | ||
reinterpret_cast<std::mutex *>(mutex)->unlock(); | ||
return 0; | ||
} |