xaizek / d2if (License: GPLv2+) (since 2018-12-07)
Simple dzen2 input formatter, which is supposed to not waste CPU time for nothing and provides a number of builtin "widgets" for displaying system information.
Commit 8dcff69bc9796bc5cde126eb4710f406611aeb0f

Thread safe Player status string update
Author: xaizek
Author date (UTC): 2016-04-20 16:47
Committer name: xaizek
Committer date (UTC): 2016-04-20 16:47
Parent(s): e8a3ce47f3d45015eb7404bc5ed144dded280db9
Signing key:
Tree: b3669134941a167f980379b00c4db91bc8829107
File Lines added Lines deleted
src/Player.cpp 13 8
src/Player.hpp 2 2
File src/Player.cpp changed (mode: 100644) (index 04928d5..2cabf74)
19 19
20 20 #include <unistd.h> #include <unistd.h>
21 21
22 #include <atomic>
22 23 #include <chrono> #include <chrono>
24 #include <memory>
23 25 #include <stdexcept> #include <stdexcept>
24 26 #include <string> #include <string>
25 27 #include <thread> #include <thread>
 
... ... Player::Player(const ColorScheme& colorScheme, std::string host, int port)
44 46 const std::string song = mpdClient.getCurrentSong(); const std::string song = mpdClient.getCurrentSong();
45 47 const MpdState state = mpdClient.getState(); const MpdState state = mpdClient.getState();
46 48
47 // XXX: this might be non thread safe
48 status = getStateMark(state) + " " + song;
49 std::string newStatus = getStateMark(state) + " " + song;
50 std::atomic_exchange(
51 &status, std::make_shared<std::string>(newStatus));
49 52
50 53 mpdClient.waitForChanges(); mpdClient.waitForChanges();
51 54 } }
52 } catch (std::runtime_error &e) {
53 std::this_thread::sleep_for(std::chrono::milliseconds(500));
54
55 // XXX: this might be non thread safe
56 status.clear();
55 } catch (std::runtime_error &) {
56 std::atomic_exchange(&status, {});
57 57 } }
58
59 // Do not consume CPU for nothing while there is no connection.
60 std::this_thread::sleep_for(std::chrono::milliseconds(500));
58 61 } }
59 62 }).detach(); }).detach();
60 63 } }
 
... ... static std::string getStateMark(MpdState state)
72 75
73 76 void Player::update() void Player::update()
74 77 { {
75 Field::setText(status);
78 if (std::shared_ptr<std::string> text = std::atomic_exchange(&status, {})) {
79 Field::setText(*text);
80 }
76 81 } }
File src/Player.hpp changed (mode: 100644) (index ad83d2e..c4acdfa)
18 18 #ifndef D2IF__PLAYER_HPP__ #ifndef D2IF__PLAYER_HPP__
19 19 #define D2IF__PLAYER_HPP__ #define D2IF__PLAYER_HPP__
20 20
21 #include <atomic>
21 #include <memory>
22 22 #include <string> #include <string>
23 23
24 24 #include "Field.hpp" #include "Field.hpp"
 
... ... private:
36 36 const std::string host; const std::string host;
37 37 const int port; const int port;
38 38
39 std::string status;
39 std::shared_ptr<std::string> status;
40 40 }; };
41 41
42 42 #endif // D2IF__PLAYER_HPP__ #endif // D2IF__PLAYER_HPP__
Hints

Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://code.reversed.top/user/xaizek/d2if

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@code.reversed.top/user/xaizek/d2if

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a pull request:
... clone the repository ...
... make some changes and some commits ...
git push origin master