Skip to content

Commit

Permalink
Merge branch 'active' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
rajamukherji committed Jul 1, 2018
2 parents c0b00bf + f0bab92 commit e69ee08
Show file tree
Hide file tree
Showing 12 changed files with 231 additions and 12 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ sources = \
target.c \
targetset.c \
targetcache.c \
targetwatch.c \
util.c \
vfs.c

Expand Down
10 changes: 10 additions & 0 deletions cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,16 @@ void cache_close() {
sqlite3_close(Cache);
}

void cache_bump_version() {
++CurrentVersion;
printf("CurrentVersion = %d\n", CurrentVersion);
char Buffer[100];
sprintf(Buffer, "REPLACE INTO info(key, value) VALUES('version', %d)", CurrentVersion);
if (sqlite3_exec(Cache, Buffer, 0, 0, 0) != SQLITE_OK) {
printf("Sqlite error: %s\n", sqlite3_errmsg(Cache));
exit(1);
}
}

void cache_hash_get(target_t *Target, int *LastUpdated, int *LastChecked, time_t *FileTime, BYTE Hash[SHA256_BLOCK_SIZE]) {
sqlite3_bind_text(HashGetStatement, 1, Target->Id, Target->IdLength, SQLITE_STATIC);
Expand Down
1 change: 1 addition & 0 deletions cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ ml_value_t *cache_expr_get(target_t *Target);
void cache_expr_set(target_t *Target, ml_value_t *Value);

extern int CurrentVersion;
void cache_bump_version();

#endif
25 changes: 23 additions & 2 deletions rabs.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <time.h>
#include <stdio.h>
#include "target.h"
#include "targetwatch.h"
#include "context.h"
#include "util.h"
#include "cache.h"
Expand All @@ -22,7 +23,6 @@ const char *SystemName = "/_minibuild_";
const char *RootPath = 0;
ml_value_t *AppendMethod;
static int EchoCommands = 0;
extern int StatusUpdates;

static stringmap_t Globals[1] = {STRINGMAP_INIT};
static stringmap_t Defines[1] = {STRINGMAP_INIT};
Expand Down Expand Up @@ -366,6 +366,7 @@ int main(int Argc, const char **Argv) {
int QueryOnly = 0;
int ListTargets = 0;
int NumThreads = 1;
int InteractiveMode = 0;
for (int I = 1; I < Argc; ++I) {
if (Argv[I][0] == '-') {
switch (Argv[I][1]) {
Expand Down Expand Up @@ -420,6 +421,15 @@ int main(int Argc, const char **Argv) {
}
break;
}
case 'i': {
InteractiveMode = 1;
break;
}
case 'w': {
targetwatch_init();
MonitorFiles = 1;
break;
}
case 't': {
GC_disable();
break;
Expand Down Expand Up @@ -448,7 +458,11 @@ int main(int Argc, const char **Argv) {
context_push("");
context_symb_set(CurrentContext, "VERSION", ml_integer(CurrentVersion));

target_threads_start(NumThreads);
if (InteractiveMode || MonitorFiles) {

} else {
target_threads_start(NumThreads);
}

load_file(concat(RootPath, SystemName, NULL));
target_t *Target;
Expand Down Expand Up @@ -478,6 +492,13 @@ int main(int Argc, const char **Argv) {
} else {
target_update(Target);
target_threads_wait(NumThreads);
if (InteractiveMode) {
target_interactive_start(NumThreads);
ml_console(rabs_ml_global, Globals);
} else if (MonitorFiles) {
target_interactive_start(NumThreads);
targetwatch_wait();
}
}
return 0;
}
1 change: 0 additions & 1 deletion rabs.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include <pthread.h>
#include "minilang.h"

extern pthread_mutex_t GlobalLock[1];
extern const char *RootPath, *SystemName;
extern __thread const char *CurrentPath;

Expand Down
109 changes: 103 additions & 6 deletions target.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "util.h"
#include "context.h"
#include "targetcache.h"
#include "targetwatch.h"
#include "cache.h"
#include <string.h>
#include <stdlib.h>
Expand Down Expand Up @@ -35,6 +36,7 @@ typedef struct target_symb_t target_symb_t;
extern const char *RootPath;
static int QueuedTargets = 0, BuiltTargets = 0, NumTargets = 0;
int StatusUpdates = 0;
int MonitorFiles = 0;

pthread_mutex_t GlobalLock[1] = {PTHREAD_MUTEX_INITIALIZER};
static pthread_cond_t TargetAvailable[1] = {PTHREAD_COND_INITIALIZER};
Expand Down Expand Up @@ -117,7 +119,7 @@ static void target_do_build(int ThreadIndex, target_t *Target) {
if (StatusUpdates) printf("\e[35m%d / %d\e[0m #%d Built \e[32m%s\e[0m at version %d\n", BuiltTargets, QueuedTargets, ThreadIndex, Target->Id, Target->LastUpdated);
targetset_foreach(Target->Affects, Target, (void *)depends_updated_fn);
memset(Target->Depends, 0, sizeof(Target->Depends));
memset(Target->Affects, 0, sizeof(Target->Affects));
//memset(Target->Affects, 0, sizeof(Target->Affects));
pthread_cond_broadcast(TargetAvailable);
}

Expand All @@ -130,13 +132,25 @@ int depends_update_fn(target_t *Depend, target_t *Target) {
if (Depend->LastUpdated == STATE_QUEUED) {
if (!targetset_insert(Depend->Affects, Target)) ++Target->WaitCount;
} else {
targetset_insert(Depend->Affects, Target);
if (Depend->LastUpdated > Target->DependsLastUpdated) {
Target->DependsLastUpdated = Depend->LastUpdated;
}
}
return 0;
}

static int affects_refresh_fn(target_t *Affect, target_t *Target) {
printf("%s -> %s\n", Target->Id, Affect->Id);
if (Affect->LastUpdated != STATE_QUEUED) {
Affect->LastUpdated = STATE_QUEUED;
++QueuedTargets;
targetset_foreach(Affect->Affects, Affect, (void *)affects_refresh_fn);
}
if (!targetset_insert(Affect->Depends, Target)) ++Affect->WaitCount;
return 0;
}

void target_update(target_t *Target) {
if (Target->LastUpdated == STATE_CHECKING) {
printf("\e[31mError: build cycle with %s\e[0m\n", Target->Id);
Expand Down Expand Up @@ -167,6 +181,15 @@ void target_update(target_t *Target) {
}
}

void target_recheck(target_t *Target) {
printf("Rechecking %s\n", Target->Id);
cache_bump_version();
++QueuedTargets;
targetset_foreach(Target->Affects, Target, (void *)affects_refresh_fn);
target_queue_build(Target);
pthread_cond_broadcast(TargetAvailable);
}

int depends_query_fn(const char *Id, target_t *Depends, int *DependsLastUpdated) {
target_query(Depends);
if (Depends->LastUpdated > *DependsLastUpdated) *DependsLastUpdated = Depends->LastUpdated;
Expand Down Expand Up @@ -297,6 +320,9 @@ static time_t target_file_hash(target_file_t *Target, time_t PreviousTime, BYTE
sha256_final(Ctx, Target->Hash);
}
pthread_mutex_lock(GlobalLock);
if (MonitorFiles && !Target->Build) {
targetwatch_add(FileName, (target_t *)Target);
}
return Stat->st_mtime;
}

Expand Down Expand Up @@ -826,12 +852,18 @@ ml_value_t *target_set_build(void *Data, int Count, ml_value_t **Args) {
return Args[0];
}

ml_value_t *target_recheck_value(void *Data, int Count, ml_value_t **Args) {
target_recheck((target_t *)Args[0]);
return Args[0];
}

struct target_scan_t {
TARGET_FIELDS
const char *Name;
target_t *Source;
scan_results_t *Results;
ml_value_t *Scan, *Rebuild;
int Recursive;
};

static ml_type_t *ScanTargetT;
Expand All @@ -843,6 +875,12 @@ struct scan_results_t {

static ml_type_t *ScanResultsT;

static int scan_results_affects_fn(target_t *Target, target_t *Scan) {
targetset_insert(Target->Affects, Scan);
targetset_insert(Scan->Depends, Target);
return 0;
}

static time_t target_scan_hash(target_scan_t *Target, time_t PreviousTime, BYTE PreviousHash[SHA256_BLOCK_SIZE]) {
targetset_t *Scans = cache_scan_get((target_t *)Target->Results);
if (Scans) targetset_foreach(Scans, Target->Results, (void *)depends_update_fn);
Expand Down Expand Up @@ -900,6 +938,9 @@ static void target_scan_build(target_scan_t *Target) {
targetset_t Scans[1] = {TARGETSET_INIT};
ml_list_foreach(Result, Scans, (void *)build_scan_target_list);
cache_depends_set((target_t *)Target->Results, Scans);
if (Target->Recursive) {
targetset_foreach(Scans, Target, (void *)scan_results_affects_fn);
}
cache_scan_set((target_t *)Target->Results, Scans);
}

Expand Down Expand Up @@ -954,8 +995,8 @@ static int scan_depends_update_fn(target_t *Depend, scan_results_t *Target) {
return 0;
}

static scan_results_t *scan_results_new(target_t *ParentTarget, const char *Name, target_t **Slot) {
const char *Id = concat("results:", ParentTarget->Id, "::", Name, NULL);
static scan_results_t *scan_results_new(target_t *ParentTarget, const char *Name, target_t **Slot, int Recursive) {
const char *Id = concat(Recursive ? "results!:" : "results:", ParentTarget->Id, "::", Name, NULL);
if (!Slot) Slot = targetcache_lookup(Id);
scan_results_t *Target = (scan_results_t *)Slot[0];
if (!Target) {
Expand All @@ -973,6 +1014,7 @@ static scan_results_t *scan_results_new(target_t *ParentTarget, const char *Name
ScanTarget->Source = ParentTarget;
ScanTarget->Results = Target;
ScanTarget->Rebuild = ml_function(ScanTarget, (void *)scan_target_rebuild);
ScanTarget->Recursive = Recursive;
targetset_insert(Target->Depends, (target_t *)ScanTarget);
}
targetset_t *Scans = cache_scan_get((target_t *)Target);
Expand All @@ -981,7 +1023,7 @@ static scan_results_t *scan_results_new(target_t *ParentTarget, const char *Name
}

ml_value_t *target_scan_new(void *Data, int Count, ml_value_t **Args) {
return (ml_value_t *)scan_results_new((target_t *)Args[0], ml_string_value(Args[1]), 0);
return (ml_value_t *)scan_results_new((target_t *)Args[0], ml_string_value(Args[1]), 0, (Count > 2) && Args[2] != MLNil);
}

struct target_symb_t {
Expand Down Expand Up @@ -1157,6 +1199,19 @@ target_t *target_find(const char *Id) {
target_expr_t *Target = target_new(target_expr_t, ExprTargetT, Id, Slot);
return (target_t *)Target;
}
if (!memcmp(Id, "results!", 8)) {
const char *Name;
for (Name = Id + strlen(Id) - 1; --Name > Id + 9;) {
if (Name[0] == ':' && Name[1] == ':') break;
}
size_t ParentIdLength = Name - Id - 9;
char *ParentId = snew(ParentIdLength + 1);
memcpy(ParentId, Id + 9, ParentIdLength);
ParentId[ParentIdLength] = 0;
target_t *ParentTarget = target_find(ParentId);
Name += 2;
return (target_t *)scan_results_new(ParentTarget, Name, Slot, 1);
}
if (!memcmp(Id, "results", 7)) {
const char *Name;
for (Name = Id + strlen(Id) - 1; --Name > Id + 8;) {
Expand All @@ -1168,7 +1223,7 @@ target_t *target_find(const char *Id) {
ParentId[ParentIdLength] = 0;
target_t *ParentTarget = target_find(ParentId);
Name += 2;
return (target_t *)scan_results_new(ParentTarget, Name, Slot);
return (target_t *)scan_results_new(ParentTarget, Name, Slot, 0);
}
return 0;
}
Expand All @@ -1194,7 +1249,7 @@ struct build_thread_t {
};

static build_thread_t *BuildThreads = 0;
int RunningThreads = 1, LastThread = 0;
int RunningThreads = 0, LastThread = 0;

static void *target_thread_fn(void *Arg) {
int Index = (int)(ptrdiff_t)Arg;
Expand Down Expand Up @@ -1227,7 +1282,33 @@ static void *target_thread_fn(void *Arg) {
return 0;
}

static void *active_mode_thread_fn(void *Arg) {
int Index = (int)(ptrdiff_t)Arg;
printf("Starting build thread #%d\n", (int)Index);
pthread_mutex_lock(GlobalLock);
++RunningThreads;
for (;;) {
while (!BuildQueue) {
//printf("[%d]: No target in build queue, %d threads running\n", Index, RunningThreads);
pthread_cond_wait(TargetAvailable, GlobalLock);
}
target_t *Target = BuildQueue;
BuildQueue = Target->Next;
Target->Next = 0;
target_do_build(Index, Target);
if (SpareThreads) {
--RunningThreads;
--SpareThreads;
pthread_cond_signal(TargetAvailable);
pthread_mutex_unlock(GlobalLock);
return 0;
}
}
return 0;
}

void target_threads_start(int NumThreads) {
RunningThreads = 1;
pthread_mutex_init(GlobalLock, NULL);
pthread_mutex_lock(GlobalLock);
pthread_cond_init(TargetAvailable, NULL);
Expand Down Expand Up @@ -1270,6 +1351,21 @@ static int print_unbuilt_target(const char *Id, target_t *Target, void *Data) {
return 0;
}

void target_interactive_start(int NumThreads) {
RunningThreads = 0;
pthread_mutex_init(GlobalLock, NULL);
pthread_mutex_lock(GlobalLock);
pthread_cond_init(TargetAvailable, NULL);
for (LastThread = 0; LastThread < NumThreads; ++LastThread) {
build_thread_t *BuildThread = new(build_thread_t);
GC_pthread_create(&BuildThread->Handle, 0, active_mode_thread_fn, (void *)(ptrdiff_t)LastThread);
BuildThread->Next = BuildThreads;
BuildThreads = BuildThread;
}
pthread_cond_broadcast(TargetAvailable);
pthread_mutex_unlock(GlobalLock);
}

void target_threads_wait(int NumThreads) {
--RunningThreads;
pthread_cond_broadcast(TargetAvailable);
Expand Down Expand Up @@ -1307,6 +1403,7 @@ void target_init() {
ml_method_by_name("string", 0, target_file_to_string, FileTargetT, NULL);
ml_method_by_name("string", 0, target_expr_to_string, ExprTargetT, NULL);
ml_method_by_name("=>", 0, target_set_build, TargetT, MLAnyT, NULL);
ml_method_by_name("refresh", 0, target_recheck_value, TargetT, NULL);
ml_method_by_name("=>", 0, scan_results_set_build, ScanResultsT, MLAnyT, NULL);
ml_method_by_name("/", 0, target_file_div, FileTargetT, MLStringT, NULL);
ml_method_by_name("%", 0, target_file_mod, FileTargetT, MLStringT, NULL);
Expand Down
6 changes: 6 additions & 0 deletions target.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ struct target_t {
TARGET_FIELDS
};

extern int StatusUpdates;
extern int MonitorFiles;
extern pthread_mutex_t GlobalLock[1];

void target_init();

ml_value_t *target_dir_new(void *Data, int Count, ml_value_t **Args);
Expand All @@ -47,5 +51,7 @@ void target_list();
target_t *target_file_check(const char *Path, int Absolute);
void target_threads_start(int NumThreads);
void target_threads_wait(int NumThreads);
void target_interactive_start(int NumThreads);
void target_recheck(target_t *Target);

#endif
Loading

0 comments on commit e69ee08

Please sign in to comment.