Skip to content

Commit

Permalink
Merge branch 'main' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
chyyuu committed Jun 18, 2023
2 parents 70c2ec9 + 4e9c267 commit fda998b
Show file tree
Hide file tree
Showing 40 changed files with 945 additions and 120 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions apps/c/pthread/basic/expect_info_smp4_fifo.out
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ Initialize global memory allocator...
Initialize kernel page table...
Initialize platform devices...
Initialize scheduling...
use FIFO scheduler.
use FIFO scheduler.
Pass NULL argument
Recieve: Main thread pass message
Child thread return message
test_create_join: Child thread return message
A message before call pthread_exit
Exit message
test_create_exit: Exit message
test_mutex: data = 100
(C)Pthread basic tests run OK!
Shutting down...
Shutting down...
56 changes: 47 additions & 9 deletions apps/c/pthread/basic/main.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include <assert.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <assert.h>
#include <pthread.h>

void *ThreadFunc1(void *arg)
{
Expand All @@ -24,6 +24,23 @@ void *ThreadFunc2(void *arg)
puts("This message should not be printed");
}

static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;

void *ThreadFunc3(void *arg)
{
pthread_mutex_lock(&lock);

int value = *(int *)arg;

// long operation
for (int i = 0; i < 100000; i++) getpid();

*(int *)arg = value + 1;

pthread_mutex_unlock(&lock);
return 0;
}

void test_create_join()
{
int res;
Expand Down Expand Up @@ -52,9 +69,7 @@ void test_create_join()
puts("Second pthread join fail");
}

char buf[128];
sprintf(buf, "%s", (char *)thread_result);
puts(buf);
printf("test_create_join: %s\n", (char *)thread_result);
}

void test_create_exit()
Expand All @@ -74,10 +89,32 @@ void test_create_exit()
puts("pthread join fail");
}

char buf[16];
sprintf(buf, "%s", (char *)thread_result);
puts(buf);
return;
printf("test_create_exit: %s\n", (char *)thread_result);
}

void test_mutex()
{
const int NUM_THREADS = 100;
int data = 0;
pthread_t t[NUM_THREADS];

for (int i = 0; i < NUM_THREADS; i++) {
int res = pthread_create(&t[i], NULL, ThreadFunc3, &data);
if (res != 0) {
puts("pthread create fail");
return;
}
}

for (int i = 0; i < NUM_THREADS; i++) {
int res = pthread_join(t[i], NULL);
if (res != 0) {
puts("pthread join fail");
}
}

printf("test_mutex: data = %d\n", data);
assert(data == NUM_THREADS);
}

int main()
Expand All @@ -87,6 +124,7 @@ int main()

test_create_join();
test_create_exit();
test_mutex();
puts("(C)Pthread basic tests run OK!");

return 0;
Expand Down
2 changes: 1 addition & 1 deletion crates/axio/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ where
}

/// Struct for poll result.
#[derive(Debug)]
#[derive(Debug, Default)]
pub struct PollState {
/// Object can be read now.
pub readable: bool,
Expand Down
2 changes: 1 addition & 1 deletion modules/axconfig/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ fn gen_config_rs(platform: &str) -> Result<()> {
Some("# Number of CPUs"),
);

println!("{config:#x?}");
// println!("{config:#x?}");

// Generate config.rs
let mut output = Vec::new();
Expand Down
1 change: 1 addition & 0 deletions modules/axruntime/src/mp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ pub extern "C" fn rust_main_secondary(cpu_id: usize) -> ! {
core::hint::spin_loop();
}

#[cfg(feature = "irq")]
axhal::arch::enable_irqs();

#[cfg(feature = "multitask")]
Expand Down
27 changes: 19 additions & 8 deletions modules/axsync/src/mutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,24 @@ impl<T: ?Sized> Mutex<T> {
}
}

/// Force unlock the [`Mutex`].
///
/// # Safety
///
/// This is *extremely* unsafe if the lock is not held by the current
/// thread. However, this can be useful in some instances for exposing
/// the lock to FFI that doesn’t know how to deal with RAII.
pub unsafe fn force_unlock(&self) {
let owner_id = self.owner_id.swap(0, Ordering::Release);
assert_eq!(
owner_id,
current().id().as_u64(),
"{} tried to release mutex it doesn't own",
current().id_name()
);
self.wq.notify_one(true);
}

/// Returns a mutable reference to the underlying data.
///
/// Since this call borrows the [`Mutex`] mutably, and a mutable reference is guaranteed to be exclusive in
Expand Down Expand Up @@ -184,14 +202,7 @@ impl<'a, T: ?Sized + fmt::Debug> fmt::Debug for MutexGuard<'a, T> {
impl<'a, T: ?Sized> Drop for MutexGuard<'a, T> {
/// The dropping of the [`MutexGuard`] will release the lock it was created from.
fn drop(&mut self) {
let owner_id = self.lock.owner_id.swap(0, Ordering::Release);
assert_eq!(
owner_id,
current().id().as_u64(),
"{} tried to release mutex it doesn't own",
current().id_name()
);
self.lock.wq.notify_one(true);
unsafe { self.lock.force_unlock() }
}
}

Expand Down
1 change: 1 addition & 0 deletions modules/axtask/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ pub fn run_idle() -> ! {
loop {
yield_now();
debug!("idle task: waiting for IRQs...");
#[cfg(feature = "irq")]
axhal::arch::wait_for_irqs();
}
}
5 changes: 3 additions & 2 deletions ulib/c_libax/build.mk
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ ulib_dir := ulib/c_libax
src_dir := $(ulib_dir)/src
obj_dir := $(ulib_dir)/build_$(ARCH)
inc_dir := $(ulib_dir)/include
libax_inc_dir = $(ulib_dir)/../libax/include
c_lib := $(obj_dir)/libc.a

in_feat := $(APP)/features.txt
Expand All @@ -14,7 +15,7 @@ ulib_src := $(wildcard $(src_dir)/*.c)
ulib_obj := $(patsubst $(src_dir)/%.c,$(obj_dir)/%.o,$(ulib_src))

CFLAGS += -nostdinc -static -no-pie -fno-builtin -ffreestanding -Wall
CFLAGS += -I$(inc_dir) -I$(ulib_dir)/../libax
CFLAGS += -I$(inc_dir) -I$(libax_inc_dir)
LDFLAGS += -nostdlib -static -no-pie --gc-sections -T$(LD_SCRIPT)

ifeq ($(ARCH), x86_64)
Expand Down Expand Up @@ -70,7 +71,7 @@ app-objs := main.o

app-objs := $(addprefix $(APP)/,$(app-objs))

$(APP)/%.o: $(APP)/%.c
$(APP)/%.o: $(APP)/%.c $(libax_inc_dir)/ax_pthread_mutex.h
$(CC) $(CFLAGS) $(APP_CFLAGS) -c -o $@ $<

$(OUT_ELF): $(app-objs) $(c_lib) $(rust_lib)
Expand Down
41 changes: 41 additions & 0 deletions ulib/c_libax/include/dirent.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#ifndef _DIRENT_H
#define _DIRENT_H

#include <sys/types.h>

struct __dirstream {
long long tell;
int fd;
int buf_pos;
int buf_end;
int lock[1];
char buf[2048];
};

typedef struct __dirstream DIR;

struct dirent {
ino_t d_ino;
off_t d_off;
unsigned short d_reclen;
unsigned char d_type;
char d_name[256];
};

int closedir(DIR *);
DIR *fdopendir(int);
int dirfd(DIR *);

#define DT_UNKNOWN 0
#define DT_FIFO 1
#define DT_CHR 2
#define DT_DIR 4
#define DT_BLK 6
#define DT_REG 8
#define DT_LNK 10
#define DT_SOCK 12
#define DT_WHT 14
#define IFTODT(x) ((x) >> 12 & 017)
#define DTTOIF(x) ((x) << 12)

#endif //_DIRENT_H
15 changes: 15 additions & 0 deletions ulib/c_libax/include/libgen.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#ifndef _LIBGEN_H
#define _LIBGEN_H

#ifdef __cplusplus
extern "C" {
#endif

char *dirname(char *);
char *basename(char *);

#ifdef __cplusplus
}
#endif

#endif
29 changes: 7 additions & 22 deletions ulib/c_libax/include/pthread.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#ifndef _PTHREAD_H
#define _PTHREAD_H 1
#define _PTHREAD_H

#include <locale.h>
#include <signal.h>
#include <stddef.h>
#include <stdint.h>
Expand All @@ -17,18 +16,7 @@ typedef struct {
unsigned __attr;
} pthread_condattr_t;

#define __DEFINED_pthread_condattr_t
#define __SIZEOF_PTHREAD_MUTEX_T 40

typedef struct {
union {
int __i[sizeof(long) == 8 ? 10 : 6];
volatile int __vi[sizeof(long) == 8 ? 10 : 6];
volatile void *volatile __p[sizeof(long) == 8 ? 5 : 6];
} __u;
} pthread_mutex_t;

#define _m_type __u.__i[0]
#include <ax_pthread_mutex.h>

typedef struct {
unsigned __attr;
Expand All @@ -55,17 +43,13 @@ typedef struct {
#define _c_clock __u.__i[4]
#define _c_shared __u.__p[0]

#define PTHREAD_MUTEX_INITIALIZER \
{ \
0 \
}

typedef void *pthread_t;

#define PTHREAD_CANCELED ((void *)-1)
#define SIGCANCEL 33

#if defined(AX_CONFIG_MULTITASK) && defined(AX_CONFIG_ALLOC)
#if defined(AX_CONFIG_MULTITASK)

_Noreturn void pthread_exit(void *);
pthread_t pthread_self(void);
int pthread_create(pthread_t *__restrict, const pthread_attr_t *__restrict, void *(*)(void *),
Expand All @@ -78,6 +62,7 @@ int pthread_setcanceltype(int, int *);
int pthread_mutex_init(pthread_mutex_t *__restrict, const pthread_mutexattr_t *__restrict);
int pthread_mutex_lock(pthread_mutex_t *);
int pthread_mutex_unlock(pthread_mutex_t *);
#endif

#endif
#endif // AX_CONFIG_MULTITASK

#endif // _PTHREAD_H
1 change: 1 addition & 0 deletions ulib/c_libax/include/stdio.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ extern FILE *const stderr;

#if defined(AX_CONFIG_ALLOC) && defined(AX_CONFIG_FS)
FILE *fopen(const char *filename, const char *mode);
char *fgets(char *__restrict, int, FILE *__restrict);
#endif

int fflush(FILE *);
Expand Down
5 changes: 5 additions & 0 deletions ulib/c_libax/include/stdlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ void *realloc(void *memblock, size_t size);
_Noreturn void abort(void);
char *getenv(const char *name);

#ifdef AX_CONFIG_FP_SIMD
float strtof(const char *__restrict, char **__restrict);
double strtod(const char *__restrict, char **__restrict);
#endif

long strtol(const char *__restrict, char **__restrict, int);
unsigned long strtoul(const char *nptr, char **endptr, int base);
long long strtoll(const char *nptr, char **endptr, int base);
Expand Down
5 changes: 5 additions & 0 deletions ulib/c_libax/include/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,13 @@ char *strncat(char *restrict d, const char *restrict s, size_t n);
int strcmp(const char *l, const char *r);
int strncmp(const char *l, const char *r, size_t n);

int strcoll(const char *, const char *);

size_t strcspn(const char *s1, const char *s2);
size_t strspn(const char *s, const char *c);
char *strpbrk(const char *, const char *);

char *strchrnul(const char *, int);

char *strrchr(const char *str, int c);
char *strchr(const char *str, int c);
Expand Down
Loading

0 comments on commit fda998b

Please sign in to comment.