Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
shiyanhui committed Apr 6, 2020
0 parents commit 48782ba
Show file tree
Hide file tree
Showing 74 changed files with 9,643 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .bumpversion.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[bumpversion]
current_version = 0.0.1
commit = True
tag = True

[bumpversion:file:VERSION]
[bumpversion:file:plugin/cli.cpp]
68 changes: 68 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
libcsp
cspcli
build
main

ar-lib
compile
configure
config.h
config.log
config.h.in
config.guess
config.sub
Makefile.in
missing
depcomp
ltmain.sh
libtool
stamp-h1
install-sh
Makefile
.dirstamp
.DS_Store

*.a
*.cache
*.cg
*.o
*.out
*.sf
*.so
*.status
*.m4
*.deps
*.la
*.libs
*.lo
*~

async
chan
infinity
long_params
mutex
netpoll
producer_consumer
sleep
sync
timer

test_chan
test_corepool
test_mem
test_proc
test_rand
test_rbq
test_rbtree
test_runq
test_timer

benchmark_sum
benchmark_sum_go

docs/data
docs/layouts
docs/public
docs/resources
docs/static
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "docs/themes/book"]
path = docs/themes/book
url = https://github.com/alex-shpak/hugo-book
17 changes: 17 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
60 changes: 60 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Copyright (c) 2020, Yanhui Shi <lime.syh at gmail dot com>
# All rights reserved.
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

PLUGIN_DIR = `$(CC) -print-file-name=plugin`
VERSION_NUMBER = `echo $(VERSION) | $(SED) -e "s/\./:/g"`
MAJOR_VERSION = `echo $(VERSION) | $(AWK) -F . '{print $$1}'`

ACLOCAL_AMFLAGS = -I m4
AM_CXXFLAGS = -I $(PLUGIN_DIR)/include
AM_CFLAGS = -fplugin=.libs/libcspplugin.so.$(MAJOR_VERSION) \
-fplugin-arg-libcspplugin.so-building-libcsp=true

bin_PROGRAMS = cspcli
lib_LTLIBRARIES = libcspplugin.la libcsp.la

cspcli_SOURCES = plugin/cli.cpp plugin/fs.hpp plugin/namer.hpp plugin/sa.hpp

libcspplugin_la_SOURCES = \
plugin/fs.hpp plugin/namer.hpp plugin/plugin.cpp plugin/proc.hpp plugin/sa.hpp

libcsp_la_SOURCES = \
src/chan.h src/common.h src/cond.h src/core.h src/core.c src/corepool.h \
src/corepool.c src/csp.h src/mem.c src/monitor.c src/mutex.h src/netpoll.h \
src/netpoll.c src/proc.h src/proc.c src/rand.h src/rand.c src/rbq.h \
src/rbtree.h src/runq.h src/runq.c src/sched.h src/sched.c src/timer.h \
src/timer.c

libcspplugin_la_LDFLAGS = -version-number $(VERSION_NUMBER)
libcsp_la_LDFLAGS = -version-number $(VERSION_NUMBER) -pthread

install-exec-hook:
$(LN_S) -f $(DESTDIR)$(libdir)/libcspplugin.so.$(VERSION) $(PLUGIN_DIR)/libcsp.so

install-data-hook:
rm -rf $(includedir)/libcsp $(datadir)/libcsp || true
$(MKDIR_P) $(includedir)/libcsp $(datadir)/libcsp
cp config.h src/chan.h src/common.h src/cond.h src/core.h src/csp.h \
src/mutex.h src/netpoll.h src/proc.h src/rbq.h src/runq.h src/sched.h \
src/timer.h $(includedir)/libcsp
cp $(WORKING_DIR)/*.sf $(WORKING_DIR)/*.cg $(WORKING_DIR)/.session $(datadir)/libcsp

uninstall-local:
rm -rf $(includedir)/libcsp
rm -rf $(datadir)/libcsp
rm -f $(PLUGIN_DIR)/libcsp.so

clean-local:
rm -rf $(WORKING_DIR)/.sf $(WORKING_DIR)/*.cg $(WORKING_DIR)/.session || true
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
## Introduction

`libcsp` is a high performance concurrency C library influenced by the
[CSP](https://en.wikipedia.org/wiki/Communicating_sequential_processes) model.

## Features

- Multiple cpu cores supported.
- High performance scheduler.
- Stack size statically analyzed in compile time.
- Lock-free channel.
- Netpoll and timer are supported.

## Documentation

Go to [https://libcsp.com](https://libcsp.com) for the documentation.

## License

Libcsp is licensed under the MIT license.
1 change: 1 addition & 0 deletions VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.0.1
49 changes: 49 additions & 0 deletions benchmarks/sum.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright (c) 2020, Yanhui Shi <lime.syh at gmail dot com>
* All rights reserved.
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

#define csp_without_prefix

#include <stdio.h>
#include <libcsp/csp.h>

#define N 10

proc void sum(int64_t low, int64_t high, int64_t *result) {
if (low == high) {
*result = low;
return;
}
int64_t mid = low + ((high - low) >> 1), left, right;
sync(sum(low, mid, &left); sum(mid + 1, high, &right));
*result = left + right;
}

int main(void) {
int64_t result = 0;
timer_time_t start, end;

start = timer_now();
for (int i = 0; i < N; i++) {
sum(0, 10000000, &result);
}
end = timer_now();

printf("The result is %ld, ran %d rounds, %lf seconds per round.\n",
result, N, (double)(end - start) / timer_second / N
);
return 0;
}
67 changes: 67 additions & 0 deletions benchmarks/sum.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright (c) 2020, Yanhui Shi <lime.syh at gmail dot com>
* All rights reserved.
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

package main

import (
"fmt"
"sync"
"time"
)

const N = 10

func sum(low, high int64, result *int64) {
if low == high {
*result = low
return
}

var left, right int64
mid := low + ((high - low) >> 1)

var wg sync.WaitGroup
wg.Add(2)

go func() {
sum(low, mid, &left)
wg.Done()
}()

go func() {
sum(mid+1, high, &right)
wg.Done()
}()

wg.Wait()

*result = left + right
}

func main() {
var result int64

start := time.Now()
for i := 0; i < N; i++ {
sum(0, 10000000, &result)
}
duration := time.Since(start)

fmt.Printf("The result is %d, ran %d rounds, %f seconds per round.\n",
result, N, float64(duration)/float64(time.Second)/float64(N),
)
}
59 changes: 59 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Copyright (c) 2020, Yanhui Shi <lime.syh at gmail dot com>
# All rights reserved.
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

AC_PREREQ([2.69])

AC_INIT([libcsp], [m4_esyscmd_s([cat VERSION])],
[https://github.com/libcsp/issues], [libcsp], [https://libcsp.com])
AM_INIT_AUTOMAKE([-Wall -Werror foreign subdir-objects])

AC_DISABLE_STATIC

AC_CONFIG_SRCDIR([src/csp.h])
AC_CONFIG_HEADERS([config.h])
AC_CONFIG_FILES([Makefile])
AC_CONFIG_MACRO_DIRS([m4])

CFLAGS="-D_GNU_SOURCE"
CXXFLAGS="-O3"

AC_ARG_ENABLE([debug], [AS_HELP_STRING([--enable-debug], [enable the debug mode])])
AS_IF([test "x$enable_debug" == xyes], [CFLAGS+=" -g -O0 -fomit-frame-pointer"], [CFLAGS+=" -O3"])

AC_ARG_ENABLE([valgrind], [AS_HELP_STRING([--enable-valgrind], [debug with valgrind])])
AS_IF([test "x$enable_valgrind" == xyes], [AC_DEFINE([csp_enable_valgrind], [], [enable valgrind])], [])

AC_ARG_WITH([sysmalloc], [AS_HELP_STRING([--with-sysmalloc], [use system malloc])])
AS_IF([test "x$with_sysmalloc" == xyes], [AC_DEFINE([csp_with_sysmalloc], [], [use system malloc])], [])

AC_PROG_CXX([g++])
AC_PROG_CC([gcc])
AC_PROG_CC_STDC
AC_PROG_AWK
AC_PROG_SED
AC_PROG_LN_S
AC_PROG_INSTALL
m4_ifdef([AM_PROG_AR], [AM_PROG_AR])

AC_SUBST([WORKING_DIR], [/tmp/libcsp])
AC_CHECK_FILE(
[$WORKING_DIR],
[rm -rf $WORKING_DIR/*.sf $WORKING_DIR/*.cg $WORKING_DIR/.session],
[$MKDIR_P $WORKING_DIR]
)

LT_INIT

AC_OUTPUT
6 changes: 6 additions & 0 deletions docs/archetypes/default.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
title: "{{ replace .Name "-" " " | title }}"
date: {{ .Date }}
draft: true
---

26 changes: 26 additions & 0 deletions docs/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
languageCode = "en-us"

baseURL = 'http://libcsp.com'
title = 'Libcsp'
theme = 'hugo-book'

disablePathToLower = true
enableGitInfo = false

[languages]
[languages.en]
languageName = 'English'
contentDir = 'content'
weight = 1

[menu]
[[menu.after]]
name = "Github"
url = "https://github.com/shiyanhui/libcsp"

[params]
BookToC = true
BookSection = '*'
BookDateFormat = 'Jan 2, 2006'
BookSearch = false
BookComments = false
Loading

0 comments on commit 48782ba

Please sign in to comment.