Skip to content

Commit

Permalink
Uploaded a version that has some bugs. Not complete.
Browse files Browse the repository at this point in the history
  • Loading branch information
ax-6 committed Aug 14, 2024
1 parent b7cf11b commit feb1ff9
Show file tree
Hide file tree
Showing 16 changed files with 133 additions and 86 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ project(AQ C)

include_directories(${PROJECT_SOURCE_DIR})

add_subdirectory(aqvm)
add_subdirectory(aq)
add_subdirectory(aq)
add_subdirectory(aqvm)
8 changes: 4 additions & 4 deletions aqvm/base/file/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ int AqvmBaseFile_LockStream(struct AqvmBaseFile_File* stream) {
return -1;
}
if (stream->file == stdin) {
if (AqvmBaseThreadingMutex_LockMutex(&AqvmBaseIo_inputMutex) != 0) {
if (AqvmBaseThreadingMutex_LockMutex(AqvmBaseIo_inputMutex) != 0) {
// TODO
return -2;
}
} else if (stream->file == stdout || stream->file == stderr) {
if (AqvmBaseThreadingMutex_LockMutex(&AqvmBaseIo_outputMutex) != 0) {
if (AqvmBaseThreadingMutex_LockMutex(AqvmBaseIo_outputMutex) != 0) {
// TODO
return -3;
}
Expand All @@ -72,12 +72,12 @@ int AqvmBaseFile_UnlockStream(struct AqvmBaseFile_File* stream) {
return -1;
}
if (stream->file == stdin) {
if (AqvmBaseThreadingMutex_UnlockMutex(&AqvmBaseIo_inputMutex) != 0) {
if (AqvmBaseThreadingMutex_UnlockMutex(AqvmBaseIo_inputMutex) != 0) {
// TODO
return -2;
}
} else if (stream->file == stdout || stream->file == stderr) {
if (AqvmBaseThreadingMutex_UnlockMutex(&AqvmBaseIo_outputMutex) != 0) {
if (AqvmBaseThreadingMutex_UnlockMutex(AqvmBaseIo_outputMutex) != 0) {
// TODO
return -3;
}
Expand Down
20 changes: 12 additions & 8 deletions aqvm/base/file/read_write_lock/read_write_lock.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,33 @@

#include "aqvm/base/file/read_write_lock/read_write_lock.h"

int AqvmBaseFileReadWriteLock_InitializeReadWriteLock(
struct AqvmBaseFileReadWriteLock_ReadWriteLock* read_write_lock) {
struct AqvmBaseFileReadWriteLock_ReadWriteLock*
AqvmBaseFileReadWriteLock_CreateReadWriteLock() {
struct AqvmBaseFileReadWriteLock_ReadWriteLock* read_write_lock =
(struct AqvmBaseFileReadWriteLock_ReadWriteLock*)malloc(
sizeof(struct AqvmBaseFileReadWriteLock_ReadWriteLock));
if (read_write_lock == NULL) {
// TODO
return -1;
return NULL;
}

if (AqvmBaseThreadingMutex_InitializeMutex(&read_write_lock->mutex) != 0) {
if (AqvmBaseThreadingMutex_CreateMutex(&read_write_lock->mutex) == NULL) {
// TODO
return -2;
return NULL;
}
read_write_lock->read_count = 0;
return 0;
}

int AqvmBaseFileReadWriteLock_CloseReadWriteLock(
int AqvmBaseFileReadWriteLock_DestroyReadWriteLock(
struct AqvmBaseFileReadWriteLock_ReadWriteLock* read_write_lock) {
if (read_write_lock == NULL) {
// TODO
return -1;
}

if (AqvmBaseThreadingMutex_CloseMutex(&read_write_lock->mutex) != 0) {
int result = AqvmBaseThreadingMutex_DestoryMutex(&read_write_lock->mutex);
free(read_write_lock);
if (result != 0) {
// TODO
return -2;
}
Expand Down
6 changes: 3 additions & 3 deletions aqvm/base/file/read_write_lock/read_write_lock.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ struct AqvmBaseFileReadWriteLock_ReadWriteLock {
int read_count;
};

int AqvmBaseFileReadWriteLock_InitializeReadWriteLock(
struct AqvmBaseFileReadWriteLock_ReadWriteLock* read_write_lock);
struct AqvmBaseFileReadWriteLock_ReadWriteLock*
AqvmBaseFileReadWriteLock_CreateReadWriteLock();

int AqvmBaseFileReadWriteLock_CloseReadWriteLock(
int AqvmBaseFileReadWriteLock_DestroyReadWriteLock(
struct AqvmBaseFileReadWriteLock_ReadWriteLock* read_write_lock);

int AqvmBaseFileReadWriteLock_LockReadLock(
Expand Down
2 changes: 2 additions & 0 deletions aqvm/base/hash/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ include_directories(${PROJECT_SOURCE_DIR})

set(SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/hash.c)

add_subdirectory(map)
add_subdirectory(table)

add_library(AqvmBaseHash STATIC ${SOURCES})

target_link_libraries(AqvmBaseHash PRIVATE AqvmBaseHashMap)
target_link_libraries(AqvmBaseHash PRIVATE AqvmBaseHashTable)
14 changes: 14 additions & 0 deletions aqvm/base/hash/map/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Copyright 2024 AQ author, All Rights Reserved.
# This program is licensed under the AQ License. You can find the AQ license in
# the root directory.

cmake_minimum_required(VERSION 3.10)

include_directories(${PROJECT_SOURCE_DIR})

set(SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/map.c)

add_library(AqvmBaseHashMap STATIC ${SOURCES})

target_link_libraries(AqvmBaseHashMap PRIVATE AqvmBaseHash)
target_link_libraries(AqvmBaseHashMap PRIVATE AqvmBaseHashTable)
10 changes: 10 additions & 0 deletions aqvm/base/hash/map/map.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright 2024 AQ author, All Rights Reserved.
// This program is licensed under the AQ License. You can find the AQ license in
// the root directory.

#include "aqvm/base/hash/map/map.h"

#include <stddef.h>
#include <stdlib.h>

#include "aqvm/base/hash/table/table.h"
22 changes: 22 additions & 0 deletions aqvm/base/hash/map/map.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright 2024 AQ author, All Rights Reserved.
// This program is licensed under the AQ License. You can find the AQ license in
// the root directory.

#ifndef AQ_AQVM_BASE_HASH_TABLE_TABLE_H_
#define AQ_AQVM_BASE_HASH_TABLE_TABLE_H_

#include <stddef.h>

#include "aqvm/base/hash/table/table.h"

struct AqvmBaseHashMap_HashMap {
size_t capacity;
size_t size;
struct AqvmBaseHashTable_HashTable* data;
};

struct AqvmBaseHashMap_HashMap* AqvmBaseHashMap_CreateHashMap(size_t capacity);

int AqvmBaseHashMap_DestroyHashMap(struct AqvmBaseHashMap_HashMap* hash_map);

#endif
17 changes: 8 additions & 9 deletions aqvm/base/io/io.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

#include "aqvm/base/file/file.h"

AqvmBaseThreadingMutex_Mutex AqvmBaseIo_inputMutex;
AqvmBaseThreadingMutex_Mutex AqvmBaseIo_outputMutex;
AqvmBaseThreadingMutex_Mutex* AqvmBaseIo_inputMutex;
AqvmBaseThreadingMutex_Mutex* AqvmBaseIo_outputMutex;

struct AqvmBaseFile_File AqvmBaseIo_stdoutStream;
struct AqvmBaseFile_File* AqvmBaseIo_stdout = &AqvmBaseIo_stdoutStream;
Expand All @@ -27,23 +27,22 @@ int AqvmBaseIo_InitializeIo() {
AqvmBaseIo_stdinStream.file = stdin;
AqvmBaseIo_stderrStream.file = stderr;

if (AqvmBaseThreadingMutex_InitializeMutex(&AqvmBaseIo_inputMutex) != 0) {
AqvmBaseIo_inputMutex = AqvmBaseThreadingMutex_CreateMutex();
AqvmBaseIo_outputMutex = AqvmBaseThreadingMutex_CreateMutex();
if (AqvmBaseIo_inputMutex == NULL || AqvmBaseIo_outputMutex == NULL) {
// TODO
return -1;
}
if (AqvmBaseThreadingMutex_InitializeMutex(&AqvmBaseIo_outputMutex) != 0) {
// TODO
return -2;
}

return 0;
}

int AqvmBaseIo_CloseIo() {
if (AqvmBaseThreadingMutex_CloseMutex(&AqvmBaseIo_inputMutex) != 0) {
if (AqvmBaseThreadingMutex_DestroyMutex(AqvmBaseIo_inputMutex) != 0) {
// TODO
return -1;
}
if (AqvmBaseThreadingMutex_CloseMutex(&AqvmBaseIo_outputMutex) != 0) {
if (AqvmBaseThreadingMutex_DestroyMutex(AqvmBaseIo_outputMutex) != 0) {
// TODO
return -1;
}
Expand Down
4 changes: 2 additions & 2 deletions aqvm/base/io/io.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
#include "aqvm/base/file/file.h"
#include "aqvm/base/threading/mutex/mutex.h"

extern AqvmBaseThreadingMutex_Mutex AqvmBaseIo_inputMutex;
extern AqvmBaseThreadingMutex_Mutex AqvmBaseIo_outputMutex;
extern AqvmBaseThreadingMutex_Mutex* AqvmBaseIo_inputMutex;
extern AqvmBaseThreadingMutex_Mutex* AqvmBaseIo_outputMutex;

extern struct AqvmBaseFile_File* AqvmBaseIo_stdout;
extern struct AqvmBaseFile_File* AqvmBaseIo_stdin;
Expand Down
35 changes: 14 additions & 21 deletions aqvm/base/threading/mutex/mutex.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,52 +4,45 @@

#include "aqvm/base/threading/mutex/mutex.h"

int AqvmBaseThreadingMutex_InitializeMutex(
AqvmBaseThreadingMutex_Mutex* mutex) {
if (mutex == NULL) {
// TODO
return -1;
}

AqvmBaseThreadingMutex_Mutex* AqvmBaseThreadingMutex_CreateMutex() {
#ifdef __unix__
if (AqvmBaseThreadingMutexUnix_InitializeMutex(mutex) != 0) {
// TODO
return -2;
}
return 0;
return AqvmBaseThreadingMutexUnix_CreateMutex();
#elif _WIN32
if (AqvmBaseThreadingMutexWindows_InitializeMutex(mutex) != 0) {
// TODO
return -2;
}
return 0;
return AqvmBaseThreadingMutexWindows_CreateMutex();
#else
// TODO(Threading): When Threading is developed, rewrite that code.
AqvmBaseThreadingMutex_Mutex* mutex = (AqvmBaseThreadingMutex_Mutex*)malloc(
sizeof(AqvmBaseThreadingMutex_Mutex));
if (mutex == NULL) {
// TODO
return NULL;
}
*mutex = false;
return 0;
return mutex;
#endif
}

int AqvmBaseThreadingMutex_CloseMutex(AqvmBaseThreadingMutex_Mutex* mutex) {
int AqvmBaseThreadingMutex_DestroyMutex(AqvmBaseThreadingMutex_Mutex* mutex) {
if (mutex == NULL) {
// TODO
return -1;
}

#ifdef __unix__
if (AqvmBaseThreadingMutexUnix_CloseMutex(mutex) != 0) {
if (AqvmBaseThreadingMutexUnix_DestroyMutex(mutex) != 0) {
// TODO
return -2;
}
return 0;
#elif _WIN32
if (AqvmBaseThreadingMutexWindows_CloseMutex(mutex) != 0) {
if (AqvmBaseThreadingMutexWindows_DestroyMutex(mutex) != 0) {
// TODO
return -2;
}
return 0;
#else
// TODO(Threading): When Threading is developed, rewrite that code.
free(mutex);
return 0;
#endif
}
Expand Down
10 changes: 5 additions & 5 deletions aqvm/base/threading/mutex/mutex.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ typedef AqvmBaseThreadingMutexWindows_Mutex AqvmBaseThreadingMutex_Mutex;
typedef bool AqvmBaseThreadingMutex_Mutex;
#endif

int AqvmBaseThreadingMutex_InitializeMutex(AqvmBaseThreadingMutex_Mutex *mutex);
AqvmBaseThreadingMutex_Mutex* AqvmBaseThreadingMutex_CreateMutex();

int AqvmBaseThreadingMutex_CloseMutex(AqvmBaseThreadingMutex_Mutex *mutex);
int AqvmBaseThreadingMutex_DestroyMutex(AqvmBaseThreadingMutex_Mutex* mutex);

int AqvmBaseThreadingMutex_LockMutex(AqvmBaseThreadingMutex_Mutex *mutex);
int AqvmBaseThreadingMutex_LockMutex(AqvmBaseThreadingMutex_Mutex* mutex);

int AqvmBaseThreadingMutex_TryLockMutex(AqvmBaseThreadingMutex_Mutex *mutex);
int AqvmBaseThreadingMutex_TryLockMutex(AqvmBaseThreadingMutex_Mutex* mutex);

int AqvmBaseThreadingMutex_UnlockMutex(AqvmBaseThreadingMutex_Mutex *mutex);
int AqvmBaseThreadingMutex_UnlockMutex(AqvmBaseThreadingMutex_Mutex* mutex);

#endif
16 changes: 9 additions & 7 deletions aqvm/base/threading/mutex/unix/mutex.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,31 @@

#include "aqvm/base/threading/mutex/unix/mutex.h"

int AqvmBaseThreadingMutexUnix_InitializeMutex(
AqvmBaseThreadingMutexUnix_Mutex *mutex) {
AqvmBaseThreadingMutexUnix_Mutex *AqvmBaseThreadingMutexUnix_CreateMutex() {
AqvmBaseThreadingMutexUnix_Mutex *mutex =
(AqvmBaseThreadingMutexUnix_Mutex *)malloc(
sizeof(AqvmBaseThreadingMutexUnix_Mutex));
if (mutex == NULL) {
// TODO
return -1;
return NULL;
}

int result = pthread_mutex_init(mutex, NULL);
if (result != 0) {
// TODO
return -2;
return NULL;
}
return 0;
return mutex;
}

int AqvmBaseThreadingMutexUnix_CloseMutex(
int AqvmBaseThreadingMutexUnix_DestroyMutex(
AqvmBaseThreadingMutexUnix_Mutex *mutex) {
if (mutex == NULL) {
// TODO
return -1;
}

int result = pthread_mutex_destroy(mutex);
free(mutex);
if (result != 0) {
// TODO
return -2;
Expand Down
7 changes: 3 additions & 4 deletions aqvm/base/threading/mutex/unix/mutex.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@

typedef pthread_mutex_t AqvmBaseThreadingMutexUnix_Mutex;

int AqvmBaseThreadingMutexUnix_InitializeMutex(
AqvmBaseThreadingMutexUnix_Mutex* mutex);
AqvmBaseThreadingMutexUnix_Mutex *AqvmBaseThreadingMutexUnix_CreateMutex();

int AqvmBaseThreadingMutexUnix_CloseMutex(
AqvmBaseThreadingMutexUnix_Mutex* mutex);
int AqvmBaseThreadingMutexUnix_DestroyMutex(
AqvmBaseThreadingMutexUnix_Mutex *mutex);

int AqvmBaseThreadingMutexUnix_LockMutex(
AqvmBaseThreadingMutexUnix_Mutex *mutex);
Expand Down
Loading

0 comments on commit feb1ff9

Please sign in to comment.