Re: Writing a simple text editor in D using a C tutorial

2023-08-29 Thread Răzvan Birișan via Digitalmars-d-learn

On Tuesday, 29 August 2023 at 16:59:57 UTC, Dejan Lekic wrote:
On Tuesday, 29 August 2023 at 16:17:56 UTC, Răzvan Birișan 
wrote:
Is there a better way to use `termios.h` inside D? Am I 
missing the point and there is a way to set these flags in D 
without using C libraries?


I would try to use termios from druntime instead.

Try: import core.sys.posix.termios;


I've made the change and that fixed the issue. Looks like there 
is also core.sys.posix.unistd, I'll use this and remove the C 
bindings that I've created.


I was only searching the official documentation and "termios" 
returned 0 results.
I also stumbled upon the unofficial documentation but didn't find 
it useful.


I now realize the I can browse the dmd source code on github...


Re: Writing a simple text editor in D using a C tutorial

2023-08-29 Thread Dejan Lekic via Digitalmars-d-learn

On Tuesday, 29 August 2023 at 16:17:56 UTC, Răzvan Birișan wrote:
Is there a better way to use `termios.h` inside D? Am I missing 
the point and there is a way to set these flags in D without 
using C libraries?


I would try to use termios from druntime instead.

Try: import core.sys.posix.termios;


Writing a simple text editor in D using a C tutorial

2023-08-29 Thread Răzvan Birișan via Digitalmars-d-learn

Hello,

I'm following this 
[tutorial](https://viewsourcecode.org/snaptoken/kilo/02.enteringRawMode.html#disable-ctrl-s-and-ctrl-q) and I'm having some trouble setting the flags.


I have this C program that works properly (exits as soon as I 
press `q` and `CTRL+C` does not kill it).


```
#include 
#include 
#include 
#include 
#include 

struct termios orig_termios;

void disableRawMode() {
  tcsetattr(STDIN_FILENO, TCSAFLUSH, &orig_termios);
}

void enableRawMode() {
  tcgetattr(STDIN_FILENO, &orig_termios);
  atexit(disableRawMode);
  struct termios raw = orig_termios;
  raw.c_lflag &= ~(ECHO | ICANON | ISIG);
  tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw);
}

int main() {

  enableRawMode();

  char c;
  while (read(STDIN_FILENO, &c, 1) == 1 && c != 'q') {
if (iscntrl(c)) {
  printf("%d\n", c);
} else {
  printf("%d ('%c')\n", c, c);
}
  }

  return 0;
}
```

I'm trying to write the equivalent program in D.

source/app.d
```
import adapter;
import core.stdc.stdlib : atexit;
import core.stdc.stdio : printf;
import core.stdc.ctype : iscntrl;

termios orig_termios;

extern (C)
{
void disableRawMode() {
tcsetattr(STDIN_FILENO, TCSANOW, &orig_termios);
}
}

void enableRawMode() {
tcgetattr(STDIN_FILENO, &orig_termios);
atexit(&disableRawMode);

termios raw = orig_termios;
// TODO flags ICANON and ISIG do not work
raw.c_lflag &= ~(ECHO | ICANON | ISIG);

tcsetattr(STDIN_FILENO, TCSANOW, &raw);
}

int main()
{
enableRawMode();

char c;
while (read(STDIN_FILENO, &c, 1) == 1 && c != 'q') {
if (iscntrl(c)) {
printf("%d\n", c);
} else {
printf("%d ('%c')\n", c, c);
}
}

return 0;
}

```

source/adapter/package.d
```
module adapter;

public import adapter.termios;
public import adapter.unistd;
```


source/adapter/termios.d
```
module adapter.termios;

import std.typecons : Typedef;

// from termios.h, 
https://github.com/openbsd/src/blob/master/sys/sys/termios.h

enum int ECHO   = 0x0008;
enum int ICANON = 0x0100;
enum int ISIG   = 0x0080;

enum int IXON   = 0x0200;
enum int IEXTEN = 0x0400;
enum int ICRNL  = 0x0100;

enum int TCSANOW = 0;
enum int TCSADRAIN = 1;
enum int TCSAFLUSH = 2;

enum int NCCS = 20;

alias tcflag_t = Typedef!uint;
alias cc_t = Typedef!ubyte;
// alias speed_t = Typedef!uint;

struct termios {
tcflag_t c_iflag;   /* input flags */
tcflag_t c_oflag;   /* output flags */
tcflag_t c_cflag;   /* control flags */
tcflag_t c_lflag;   /* local flags */
cc_t[NCCS] c_cc;/* control chars */
int c_ispeed;   /* input speed */
int c_ospeed;   /* output speed */
};

extern (C) int tcgetattr(int, termios *);
extern (C) int tcsetattr(int, int, const termios *);

```

source/adapter/unistd.d
```
module adapter.unistd;

import core.sys.posix.sys.types : ssize_t;

// TODO fix import
enum int STDIN_FILENO = 0;
// import core.stdc.stdio : STDIN_FILENO;

// from unistd.h
extern (C) ssize_t read(int fildes, void *buf, size_t nbytes);
```

When I try to run it with `dub run` the only flag that appears to 
be set is `~(ECHO)`. It does exit when I press `CTRL+C` and it 
does not exit as soon as I press `q`, I must follow that with an 
`ENTER`.


Is there a better way to use `termios.h` inside D? Am I missing 
the point and there is a way to set these flags in D without 
using C libraries?


Re: parallel threads stalls until all thread batches are finished.

2023-08-29 Thread Joe--- via Digitalmars-d-learn

On Monday, 28 August 2023 at 22:43:56 UTC, Ali Çehreli wrote:

On 8/28/23 15:37, j...@bloow.edu wrote:

> Basically everything is hard coded to use totalCPU's

parallel() is a function that dispatches to a default TaskPool 
object, which uses totalCPUs. It's convenient but as you say, 
not all problems should use it.


In such cases, you would create your own TaskPool object and 
call .parallel on it:


  https://youtu.be/dRORNQIB2wA?t=1611s

Ali


Thanks. Seems to work. Didn't realize it was that easy ;)


Re: parallel threads stalls until all thread batches are finished.

2023-08-29 Thread Christian Köstlin via Digitalmars-d-learn

On 29.08.23 00:37, j...@bloow.edu wrote:
Well, I have 32 cores so that would spawn 64-1 threads with hyper 
threading so not really a solution as it is too many simultaneous downs 
IMO.



"These properties get and set the number of worker threads in the 
TaskPool instance returned by taskPool. The default value is totalCPUs - 
1. Calling the setter after the first call to taskPool does not changes 
number of worker threads in the instance returned by taskPool. "


I guess I could try to see if I can change this but I don't know what 
the "first call" is(and I'm using parallel to create it).
The call that is interesting in your case is the call to `taskPool` in 
your foreach loop. So if you call `defaultPoolThreads(8)` before your 
loop you should be good.


Seems that the code should simply be made more robust. Probably a just a 
few lines of code to change/add at most. Maybe the constructor and 
parallel should take an argument to set the "totalCPUs" which defaults 
to getting the total number rather than it being hard coded.


I currently don't need or have 32+ downlaods to test ATM so...


    this() @trusted
     {
     this(totalCPUs - 1);
     }

     /**
     Allows for custom number of worker threads.
     */
     this(size_t nWorkers) @trusted
     {


Basically everything is hard coded to use totalCPU's and that is the 
ultimate problem. Not all tasks should use all CPU' >

What happens when we get 128 cores? or even 32k at some poin >
It shouldn't be a hard coded value, it's really that simple and where 
the problem originates because someone didn't think ahead.

You have the option to not use the default value.

Kind regards,
Christian