forked from syswonder/ruxos
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add
pthread_key_*
and pthread_*specific
series syscalls to suppor…
…t TSD
- Loading branch information
Showing
24 changed files
with
546 additions
and
18 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* Copyright (c) [2023] [Syswonder Community] | ||
* [Rukos] is licensed under Mulan PSL v2. | ||
* You can use this software according to the terms and conditions of the Mulan PSL v2. | ||
* You may obtain a copy of Mulan PSL v2 at: | ||
* http://license.coscl.org.cn/MulanPSL2 | ||
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. | ||
* See the Mulan PSL v2 for more details. | ||
*/ | ||
|
||
use crate::ctypes; | ||
use axerrno::LinuxError; | ||
use axtask::tsd::DestrFunction; | ||
use core::ffi::{c_int, c_void}; | ||
|
||
/// Allocate a specific key for a process shared by all threads. | ||
pub unsafe fn sys_pthread_key_create( | ||
key: *mut ctypes::pthread_key_t, | ||
destr_function: Option<DestrFunction>, | ||
) -> c_int { | ||
debug!("sys_pthread_key_create <= {:#x}", key as usize); | ||
syscall_body!(sys_pthread_key_create, { | ||
if let Some(k) = axtask::current().alloc_key(destr_function) { | ||
unsafe { | ||
*key = k as ctypes::pthread_key_t; | ||
} | ||
Ok(0) | ||
} else { | ||
Err(LinuxError::EAGAIN) | ||
} | ||
}) | ||
} | ||
|
||
/// Destroy a specific key for a process. | ||
pub fn sys_pthread_key_delete(key: ctypes::pthread_key_t) -> c_int { | ||
debug!("sys_pthread_key_delete <= {}", key); | ||
syscall_body!(sys_pthread_key_delete, { | ||
if let Some(_) = axtask::current().free_key(key as usize) { | ||
Ok(0) | ||
} else { | ||
Err(LinuxError::EINVAL) | ||
} | ||
}) | ||
} | ||
|
||
/// Set the value of a specific key for a thread. | ||
pub fn sys_pthread_setspecific(key: ctypes::pthread_key_t, value: *const c_void) -> c_int { | ||
debug!("sys_pthread_setspecific <= {}, {:#x}", key, value as usize); | ||
syscall_body!(sys_pthread_setspecific, { | ||
if let Some(_) = axtask::current().set_tsd(key as usize, value as *mut c_void) { | ||
Ok(0) | ||
} else { | ||
Err(LinuxError::EINVAL) | ||
} | ||
}) | ||
} | ||
|
||
/// Get the value of a specific key for a thread. | ||
pub fn sys_pthread_getspecific(key: ctypes::pthread_key_t) -> *mut c_void { | ||
debug!("sys_pthread_getspecific <= {}", key); | ||
syscall_body!(sys_pthread_getspecific, { | ||
if let Some(tsd) = axtask::current().get_tsd(key as usize) { | ||
Ok(tsd) | ||
} else { | ||
// return null | ||
Ok(core::ptr::null_mut()) | ||
} | ||
}) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
alloc | ||
paging | ||
multitask | ||
irq |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
smp = 4 | ||
build_mode = release | ||
log_level = info | ||
|
||
CPU 0 started | ||
Found physcial memory regions: | ||
.text (READ | EXECUTE | RESERVED) | ||
.rodata (READ | RESERVED) | ||
.data .tdata .tbss .percpu (READ | WRITE | RESERVED) | ||
.percpu (READ | WRITE | RESERVED) | ||
boot stack (READ | WRITE | RESERVED) | ||
.bss (READ | WRITE | RESERVED) | ||
free memory (READ | WRITE | FREE) | ||
Initialize global memory allocator... | ||
Initialize kernel page table... | ||
Initialize platform devices... | ||
Initialize scheduling... | ||
use FIFO scheduler. | ||
max_keys = 1024, got No.0 | ||
TSD test success | ||
(C)Pthread TSD tests run OK! | ||
Shutting down... |
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,4 @@ | ||
alloc | ||
paging | ||
multitask | ||
irq |
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,74 @@ | ||
/* Copyright (c) [2023] [Syswonder Community] | ||
* [Rukos] is licensed under Mulan PSL v2. | ||
* You can use this software according to the terms and conditions of the Mulan PSL v2. | ||
* You may obtain a copy of Mulan PSL v2 at: | ||
* http://license.coscl.org.cn/MulanPSL2 | ||
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS | ||
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A | ||
* PARTICULAR PURPOSE. See the Mulan PSL v2 for more details. | ||
*/ | ||
|
||
#include <assert.h> | ||
#include <pthread.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <unistd.h> | ||
|
||
static pthread_key_t p_key; | ||
|
||
void *specific_func(void *arg) | ||
{ | ||
int *p = (int *)malloc(sizeof(int)); | ||
*p = *(int *)arg; | ||
pthread_setspecific(p_key, p); | ||
sleep(1); | ||
int *tmp = (int *)pthread_getspecific(p_key); | ||
assert(*tmp == *(int *)arg); | ||
assert(pthread_getspecific(999999) == NULL); | ||
return NULL; | ||
} | ||
|
||
int res = 0; | ||
|
||
void destr_func(void *arg) | ||
{ | ||
res += *(int *)arg; | ||
free(arg); | ||
// It seems that printing in destr_func will cause deadlock | ||
// char *buf[100]; | ||
// sprintf(buf, "destr_func: %d", *(int *)arg); | ||
// puts(buf); | ||
} | ||
|
||
void test_specific() | ||
{ | ||
int max_keys = sysconf(_SC_THREAD_KEYS_MAX); | ||
pthread_key_create(&p_key, destr_func); | ||
printf("max_keys = %d, got No.%d\n", max_keys, p_key); | ||
|
||
pthread_t t1, t2; | ||
int arg1 = 0x1234, arg2 = 0x5678; | ||
pthread_create(&t1, NULL, specific_func, &arg1); | ||
pthread_create(&t2, NULL, specific_func, &arg2); | ||
pthread_join(t1, NULL); | ||
pthread_join(t2, NULL); | ||
if (res != 0x1234 + 0x5678) { | ||
puts("TSD test fail"); | ||
} else { | ||
puts("TSD test success"); | ||
} | ||
|
||
pthread_key_delete(p_key); | ||
} | ||
|
||
int main() | ||
{ | ||
pthread_t main_thread = pthread_self(); | ||
assert(main_thread != 0); | ||
|
||
test_specific(); | ||
|
||
puts("(C)Pthread TSD tests run OK!"); | ||
|
||
return 0; | ||
} |
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,2 @@ | ||
test_one "SMP=4 LOG=info" "expect_info_smp4_fifo.out" | ||
rm -f $APP/*.o |
Oops, something went wrong.