Skip to content
Snippets Groups Projects
Commit b24b1e07 authored by dylan.frei's avatar dylan.frei
Browse files

Added mutex for concurrency

parent 6d82d7b1
No related branches found
No related tags found
No related merge requests found
...@@ -18,9 +18,13 @@ import ( ...@@ -18,9 +18,13 @@ import (
"path/filepath" "path/filepath"
"strconv" "strconv"
"strings" "strings"
"sync"
"syscall" "syscall"
"time"
) )
var m sync.Mutex
func main() { func main() {
if _, err := os.Stat("/var/lib/image-viewer"); err != nil { if _, err := os.Stat("/var/lib/image-viewer"); err != nil {
...@@ -86,9 +90,9 @@ func Stop(c echo.Context) error { ...@@ -86,9 +90,9 @@ func Stop(c echo.Context) error {
} }
func Upload(c echo.Context) error { func Upload(c echo.Context) error {
//----------- //protect ressources
// Read file m.Lock()
//----------- defer m.Unlock()
// Source // Source
file, err := c.FormFile("file") file, err := c.FormFile("file")
...@@ -139,6 +143,10 @@ func List(c echo.Context) error { ...@@ -139,6 +143,10 @@ func List(c echo.Context) error {
} }
func Del(c echo.Context) error { func Del(c echo.Context) error {
//protect ressources
m.Lock()
defer m.Unlock()
// Removing file from the directory // Removing file from the directory
// Using Remove() function // Using Remove() function
img := c.Param("img") img := c.Param("img")
...@@ -150,6 +158,9 @@ func Del(c echo.Context) error { ...@@ -150,6 +158,9 @@ func Del(c echo.Context) error {
} }
func Display(c echo.Context) error { func Display(c echo.Context) error {
//protect ressources
m.Lock()
defer m.Unlock()
//Check if program is running //Check if program is running
if _, err := os.Stat("/var/run/image-viewer.pid"); err != nil { if _, err := os.Stat("/var/run/image-viewer.pid"); err != nil {
...@@ -187,6 +198,10 @@ func Display(c echo.Context) error { ...@@ -187,6 +198,10 @@ func Display(c echo.Context) error {
} }
syscall.Kill(pid, syscall.SIGUSR1) syscall.Kill(pid, syscall.SIGUSR1)
//Ensure image has the time to be displayed before mutex release
//In case a subsequent call to display would be made before the signal is processed by the backend,
//the data from current.ppm could be corrupted (or too long) during process.
time.Sleep(500 * time.Millisecond)
return c.String(http.StatusOK, img+" is being displayed\n") return c.String(http.StatusOK, img+" is being displayed\n")
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment