From f4ea5b63f3539de1e2f08257db43cb2002ec65d0 Mon Sep 17 00:00:00 2001 From: Pacien Date: Mon, 1 Jul 2013 15:46:54 +0200 Subject: Fix go routines sync delays --- common.go | 4 ++-- compiled.go | 5 +---- interactive.go | 7 +++---- 3 files changed, 6 insertions(+), 10 deletions(-) diff --git a/common.go b/common.go index 56bffd1..d8e2d28 100644 --- a/common.go +++ b/common.go @@ -84,7 +84,6 @@ func parse(dirPath string, elements map[string][]byte, exts []string, overwrite } func compile(dirPath string, elements map[string][]byte, sourceDir, outputDir, saveAs string, exts []string, recursive bool) { - wait.Add(1) defer wait.Done() if strings.HasPrefix(dirPath, outputDir) { @@ -96,6 +95,7 @@ func compile(dirPath string, elements map[string][]byte, sourceDir, outputDir, s if recursive { dirs, _ := fcmd.Ls(dirPath) for _, dir := range dirs { + wait.Add(1) go compile(path.Join(dirPath, dir), elements, sourceDir, outputDir, saveAs, exts, recursive) } } @@ -111,7 +111,6 @@ func compile(dirPath string, elements map[string][]byte, sourceDir, outputDir, s } func copyFiles(dirPath, sourceDir, outputDir string, exts []string, recursive bool) { - wait.Add(1) defer wait.Done() if strings.HasPrefix(dirPath, outputDir) { @@ -130,6 +129,7 @@ func copyFiles(dirPath, sourceDir, outputDir string, exts []string, recursive bo if recursive { for _, dir := range dirs { + wait.Add(1) go copyFiles(path.Join(dirPath, dir), sourceDir, outputDir, exts, recursive) } } diff --git a/compiled.go b/compiled.go index 1cd391e..e7b7d0b 100644 --- a/compiled.go +++ b/compiled.go @@ -22,7 +22,6 @@ package main import ( "fmt" "os" - "time" ) func compiled(sourceDir, outputDir string, exts []string, saveAs string) { @@ -34,12 +33,10 @@ func compiled(sourceDir, outputDir string, exts []string, saveAs string) { } // compile everything + wait.Add(2) go compile(sourceDir, make(map[string][]byte), sourceDir, outputDir, saveAs, exts, true) go copyFiles(sourceDir, sourceDir, outputDir, exts, true) - // sleep some milliseconds to prevent early exit - time.Sleep(time.Millisecond * 100) - // wait until all tasks are completed wait.Wait() fmt.Println("Compilation done.") diff --git a/interactive.go b/interactive.go index 1c9ca71..1baa4cc 100644 --- a/interactive.go +++ b/interactive.go @@ -26,7 +26,6 @@ import ( "os" "path" "strings" - "time" ) func watch(dirPath string, watcher *fsnotify.Watcher) *fsnotify.Watcher { @@ -107,21 +106,21 @@ func interactive(sourceDir, outputDir string, exts []string, saveAs string) { if fcmd.IsDir(ev.Name) { elements := parseParents(ev.Name, sourceDir, exts) dirPath := path.Join(sourceDir, strings.TrimPrefix(ev.Name, sourceDir)) + wait.Add(2) go compile(dirPath, elements, sourceDir, outputDir, saveAs, exts, true) go copyFiles(dirPath, sourceDir, outputDir, exts, true) } else { dirPath := path.Join(sourceDir, strings.TrimPrefix(dir, sourceDir)) if isParsable(path.Ext(ev.Name), exts) { elements := parseParents(dir, sourceDir, exts) + wait.Add(1) go compile(dirPath, elements, sourceDir, outputDir, saveAs, exts, true) } + wait.Add(1) go copyFiles(dirPath, sourceDir, outputDir, exts, false) } } - // sleep some milliseconds to prevent early exit - time.Sleep(time.Millisecond * 100) - // wait until all tasks are completed wait.Wait() -- cgit v1.2.3