Print examples correctly

This commit is contained in:
gingerBill
2022-01-19 16:35:50 +00:00
parent 07ee23f817
commit b5754b6ed9
3 changed files with 83 additions and 77 deletions
+36 -37
View File
@@ -1,44 +1,43 @@
package sync package sync
// A barrier enabling multiple threads to synchronize the beginning of some computation
/* /*
* Example: A barrier enabling multiple threads to synchronize the beginning of some computation
* Example:
* package example
* package example
* import "core:fmt"
* import "core:sync" import "core:fmt"
* import "core:thread" import "core:sync"
* import "core:thread"
* barrier := &sync.Barrier{};
* barrier := &sync.Barrier{};
* main :: proc() {
* fmt.println("Start"); main :: proc() {
* fmt.println("Start");
* THREAD_COUNT :: 4;
* threads: [THREAD_COUNT]^thread.Thread; THREAD_COUNT :: 4;
* threads: [THREAD_COUNT]^thread.Thread;
* sync.barrier_init(barrier, THREAD_COUNT);
* defer sync.barrier_destroy(barrier); sync.barrier_init(barrier, THREAD_COUNT);
* defer sync.barrier_destroy(barrier);
*
* for _, i in threads {
* threads[i] = thread.create_and_start(proc(t: ^thread.Thread) { for _, i in threads {
* // Same messages will be printed together but without any interleaving threads[i] = thread.create_and_start(proc(t: ^thread.Thread) {
* fmt.println("Getting ready!"); // Same messages will be printed together but without any interleaving
* sync.barrier_wait(barrier); fmt.println("Getting ready!");
* fmt.println("Off their marks they go!"); sync.barrier_wait(barrier);
* }); fmt.println("Off their marks they go!");
* } });
* }
* for t in threads {
* thread.destroy(t); // join and free thread for t in threads {
* } thread.destroy(t); // join and free thread
* fmt.println("Finished"); }
* } fmt.println("Finished");
* }
*/ */
Barrier :: struct { Barrier :: struct {
mutex: Blocking_Mutex, mutex: Blocking_Mutex,
cond: Condition, cond: Condition,
+34 -37
View File
@@ -67,44 +67,41 @@ wait_group_wait_with_timeout :: proc(wg: ^Wait_Group, duration: time.Duration) -
// A barrier enabling multiple threads to synchronize the beginning of some computation
/* /*
* Example: A barrier enabling multiple threads to synchronize the beginning of some computation
*
* package example Example:
* package example
* import "core:fmt"
* import "core:sync" import "core:fmt"
* import "core:thread" import "core:sync"
* import "core:thread"
* barrier := &sync.Barrier{}
* barrier := &sync.Barrier{}
* main :: proc() {
* fmt.println("Start") main :: proc() {
* fmt.println("Start")
* THREAD_COUNT :: 4
* threads: [THREAD_COUNT]^thread.Thread THREAD_COUNT :: 4
* threads: [THREAD_COUNT]^thread.Thread
* sync.barrier_init(barrier, THREAD_COUNT)
* defer sync.barrier_destroy(barrier) sync.barrier_init(barrier, THREAD_COUNT)
*
* for _, i in threads {
* for _, i in threads { threads[i] = thread.create_and_start(proc(t: ^thread.Thread) {
* threads[i] = thread.create_and_start(proc(t: ^thread.Thread) { // Same messages will be printed together but without any interleaving
* // Same messages will be printed together but without any interleaving fmt.println("Getting ready!")
* fmt.println("Getting ready!") sync.barrier_wait(barrier)
* sync.barrier_wait(barrier) fmt.println("Off their marks they go!")
* fmt.println("Off their marks they go!") })
* }) }
* }
* for t in threads {
* for t in threads { thread.destroy(t) // join and free thread
* thread.destroy(t) // join and free thread }
* } fmt.println("Finished")
* fmt.println("Finished") }
* } */
*
*/
Barrier :: struct { Barrier :: struct {
mutex: Mutex, mutex: Mutex,
cond: Cond, cond: Cond,
+13 -3
View File
@@ -802,21 +802,31 @@ write_docs :: proc(w: io.Writer, pkg: ^doc.Pkg, docs: string) {
it := docs it := docs
was_code := true was_code := true
was_paragraph := true was_paragraph := true
prev_line: string
for line in strings.split_iterator(&it, "\n") { for line in strings.split_iterator(&it, "\n") {
text := strings.trim_space(line)
defer prev_line = line
if strings.has_prefix(line, "\t") { if strings.has_prefix(line, "\t") {
if !was_code { if !was_code {
was_code = true; was_code = true;
fmt.wprint(w, `<pre class="doc-code"><code>`) if prev_line == "Example:" {
fmt.wprint(w, `<pre class="doc-code doc-code-example"><code>`)
} else {
fmt.wprint(w, `<pre class="doc-code"><code>`)
}
} }
fmt.wprintf(w, "%s\n", strings.trim_prefix(line, "\t")) fmt.wprintf(w, "%s\n", strings.trim_prefix(line, "\t"))
continue continue
} else if was_code { } else if was_code {
if text == "" {
continue
}
was_code = false was_code = false
fmt.wprintln(w, "</code></pre>") fmt.wprintln(w, "</code></pre>")
continue
} }
text := strings.trim_space(line)
if text == "" { if text == "" {
if was_paragraph { if was_paragraph {
was_paragraph = false was_paragraph = false
fmt.wprintln(w, "</p>") fmt.wprintln(w, "</p>")