Skip to content

Commit

Permalink
Merge branch 'c3lang:master' into patch-1
Browse files Browse the repository at this point in the history
  • Loading branch information
Book-reader authored Jan 22, 2025
2 parents 62c5ff4 + a845a93 commit 9d1544e
Show file tree
Hide file tree
Showing 38 changed files with 1,222 additions and 235 deletions.
34 changes: 17 additions & 17 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ jobs:
run: |
call "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Auxiliary\Build\vcvarsall.bat" x64
cd resources/testproject
..\..\build\${{ matrix.build_type }}\c3c.exe -vvv --emit-llvm run hello_world_win32
..\..\build\${{ matrix.build_type }}\c3c.exe -vvv --emit-llvm run hello_world_win32 --trust=full
dir build\llvm_ir
..\..\build\${{ matrix.build_type }}\c3c.exe clean
dir build\llvm_ir
Expand All @@ -61,7 +61,7 @@ jobs:
run: |
cd resources/testproject
call "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Auxiliary\Build\vcvarsall.bat" x64
..\..\build\${{ matrix.build_type }}\c3c.exe -vvv build hello_world_win32_lib
..\..\build\${{ matrix.build_type }}\c3c.exe -vvv build hello_world_win32_lib --trust=full
- name: Compile and run dynlib-test
run: |
Expand Down Expand Up @@ -132,8 +132,8 @@ jobs:
install: git binutils mingw-w64-x86_64-clang mingw-w64-x86_64-ninja mingw-w64-x86_64-cmake mingw-w64-x86_64-toolchain mingw-w64-x86_64-python
- shell: msys2 {0}
run: |
pacman --noconfirm -U https://mirror.msys2.org/mingw/mingw64/mingw-w64-x86_64-llvm-19.1.6-1-any.pkg.tar.zst
pacman --noconfirm -U https://mirror.msys2.org/mingw/mingw64/mingw-w64-x86_64-lld-19.1.6-1-any.pkg.tar.zst
pacman --noconfirm -U https://mirror.msys2.org/mingw/mingw64/mingw-w64-x86_64-llvm-19.1.7-1-any.pkg.tar.zst
pacman --noconfirm -U https://mirror.msys2.org/mingw/mingw64/mingw-w64-x86_64-lld-19.1.7-1-any.pkg.tar.zst
- name: CMake
run: |
cmake -B build -G Ninja -DCMAKE_C_COMPILER=clang -DCMAKE_BUILD_TYPE=${{ matrix.build_type }}
Expand All @@ -153,7 +153,7 @@ jobs:
- name: Build testproject
run: |
cd resources/testproject
../../build/c3c run -vvv
../../build/c3c run -vvv --trust=full
- name: Vendor-fetch
run: |
Expand All @@ -162,7 +162,7 @@ jobs:
- name: Build testproject lib
run: |
cd resources/testproject
../../build/c3c build hello_world_lib --cc cc -vvv
../../build/c3c build hello_world_lib --cc cc -vvv --trust=full
- name: run compiler tests
run: |
Expand Down Expand Up @@ -209,12 +209,12 @@ jobs:
- name: Build testproject
run: |
cd resources/testproject
../../build/c3c run -vvv
../../build/c3c run -vvv --trust=full
- name: Build testproject lib
run: |
cd resources/testproject
../../build/c3c build hello_world_lib -vvv
../../build/c3c build hello_world_lib -vvv --trust=full
- name: run compiler tests
run: |
Expand Down Expand Up @@ -349,7 +349,7 @@ jobs:
- name: Build testproject
run: |
cd resources/testproject
../../build/c3c run -vvv
../../build/c3c run -vvv --trust=full
- name: Test WASM
run: |
Expand All @@ -368,7 +368,7 @@ jobs:
- name: Build testproject direct linker
run: |
cd resources/testproject
../../build/c3c run -vvv --linker=builtin
../../build/c3c run -vvv --linker=builtin --trust=full
- name: Init a library & a project
run: |
Expand Down Expand Up @@ -491,12 +491,12 @@ jobs:
- name: Build testproject
run: |
cd resources/testproject
../../build/c3c run -vvv
../../build/c3c run -vvv --trust=full
- name: Build testproject direct linker
run: |
cd resources/testproject
../../build/c3c run -vvv --linker=builtin
../../build/c3c run -vvv --linker=builtin --trust=full
- name: run compiler tests
run: |
Expand Down Expand Up @@ -583,7 +583,7 @@ jobs:
- name: Build testproject
run: |
cd resources/testproject
../../build/c3c run -vvv
../../build/c3c run -vvv --trust=full
- name: Test WASM
run: |
Expand All @@ -593,7 +593,7 @@ jobs:
- name: Build testproject direct linker
run: |
cd resources/testproject
../../build/c3c run -vvv --linker=builtin
../../build/c3c run -vvv --linker=builtin --trust=full
- name: Init a library & a project
run: |
Expand Down Expand Up @@ -670,17 +670,17 @@ jobs:
- name: Build testproject
run: |
cd resources/testproject
../../build/c3c run -vvv
../../build/c3c run -vvv --trust=full
- name: Build testproject direct linker
run: |
cd resources/testproject
../../build/c3c run -vvv --linker=builtin
../../build/c3c run -vvv --linker=builtin --trust=full
- name: Build testproject lib
run: |
cd resources/testproject
../../build/c3c build hello_world_lib -vvv
../../build/c3c build hello_world_lib -vvv --trust=full
- name: run compiler tests
run: |
Expand Down
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,8 @@ TAGS
result

# macOS
.DS_Store
.DS_Store

# tests
/test/tmp/*
/test/testrun
2 changes: 1 addition & 1 deletion lib/std/libc/libc.c3
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ extern fn double strtod(char* str, char** endptr);
extern fn float strtof(char* str, char** endptr);
extern fn ZString strtok(ZString str, ZString delim);
extern fn CLong strtol(char* str, char** endptr, CInt base);
extern fn CULong strtul(char* str, char** endptr, CInt base);
extern fn CULong strtoul(char* str, char** endptr, CInt base);
extern fn usz strxfrm(char* dest, ZString src, usz n);
extern fn CInt system(ZString str);
extern fn Time_t timegm(Tm *timeptr) @if(!env::WIN32);
Expand Down
165 changes: 165 additions & 0 deletions lib/std/threads/buffered_channel.c3
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
module std::thread::channel(<Type>);

distinct BufferedChannel = void*;

struct BufferedChannelImpl @private
{
Allocator allocator;
Mutex mu;
bool closed;
usz size;
usz elems;

usz sendx;
usz send_waiting;
ConditionVariable send_cond;

usz readx;
usz read_waiting;
ConditionVariable read_cond;

Type[*] buf;
}

fn void! BufferedChannel.new_init(&self, usz size = 1)
{
return self.init(size, allocator::heap());
}

fn void! BufferedChannel.init(&self, usz size = 1, Allocator allocator)
{
BufferedChannelImpl* channel = allocator::new_with_padding(allocator, BufferedChannelImpl, Type.sizeof * size)!;
defer catch allocator::free(allocator, channel);

channel.allocator = allocator;
channel.size = size;
channel.elems = 0;
channel.sendx = 0;
channel.send_waiting = 0;
channel.readx = 0;
channel.read_waiting = 0;

channel.mu.init()!;
defer catch (void)channel.mu.destroy();
channel.send_cond.init()!;
defer catch (void)channel.send_cond.destroy();
channel.read_cond.init()!;
defer catch (void)channel.read_cond.destroy();

*self = (BufferedChannel)channel;
}

fn void! BufferedChannel.destroy(&self)
{
BufferedChannelImpl* channel = (BufferedChannelImpl*)(*self);

anyfault err = @catch(channel.mu.destroy());
err = @catch(channel.send_cond.destroy()) ?: err;
err = @catch(channel.read_cond.destroy()) ?: err;
allocator::free(channel.allocator, channel);

*self = null;

if (err) return err?;
}

fn void! BufferedChannel.push(self, Type val)
{
BufferedChannelImpl* channel = (BufferedChannelImpl*)self;

channel.mu.lock()!;
defer catch (void)channel.mu.unlock();

// if channel is full -> wait
while (channel.elems == channel.size && !channel.closed)
{
channel.send_waiting++;
channel.send_cond.wait(&channel.mu)!;
channel.send_waiting--;
}

// check if channel is closed
if (channel.closed) return ThreadFault.CHANNEL_CLOSED?;

// save value to buf
channel.buf[channel.sendx] = val;

// move pointer
channel.sendx++;
if (channel.sendx == channel.size)
{
channel.sendx = 0;
}

// channelge elems counter
channel.elems++;

// if someone is waiting -> awake him
if (channel.read_waiting > 0)
{
channel.read_cond.signal()!;
}

channel.mu.unlock()!;
}

fn Type! BufferedChannel.pop(self)
{
BufferedChannelImpl* channel = (BufferedChannelImpl*)self;

channel.mu.lock()!;
defer catch (void)channel.mu.unlock();

// if chan is empty -> wait for sender
while (channel.elems == 0 && !channel.closed)
{
channel.read_waiting++;
channel.read_cond.wait(&channel.mu)!;
channel.read_waiting--;
}

// check if chan is closed and empty
if (channel.closed && channel.elems == 0)
{
return ThreadFault.CHANNEL_CLOSED?;
}

// read from buf
Type ret = channel.buf[channel.readx];

// move pointer
channel.readx++;
if (channel.readx == channel.size)
{
channel.readx = 0;
}

// change elems counter
channel.elems--;

// if someone is waiting -> awake him
if (channel.send_waiting > 0)
{
channel.send_cond.signal()!;
}

channel.mu.unlock()!;

return ret;
}

fn void! BufferedChannel.close(self)
{
BufferedChannelImpl* channel = (BufferedChannelImpl*)self;

anyfault err = @catch(channel.mu.lock());

channel.closed = true;

err = @catch(channel.read_cond.broadcast()) ?: err;
err = @catch(channel.send_cond.broadcast()) ?: err;
err = @catch(channel.mu.unlock()) ?: err;

if (err) return err?;
}

1 change: 1 addition & 0 deletions lib/std/threads/thread.c3
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ fault ThreadFault
DETACH_FAILED,
JOIN_FAILED,
INTERRUPTED,
CHANNEL_CLOSED,
}

macro void! Mutex.init(&mutex) => NativeMutex.init((NativeMutex*)mutex, MUTEX_PLAIN);
Expand Down
Loading

0 comments on commit 9d1544e

Please sign in to comment.